FAQ
They are still pointing to the same underlying string.
http://blog.golang.org/slices
On Mon, Dec 8, 2014, 8:54 AM null wrote:

Why Golang decides the "Sting()" argument is actually a copy but not the reference?


package main


import "fmt"


type Student struct {

name string

age int

}


func (s Student) String() string {

fmt.Printf("%p\n", &s) //It shows that the s is actually a copy, if the function is declared as "func (s *Stude///nt) String() string" then it will not be called

return fmt.Sprintf("name is %s\n", s.name)

}


func main() {

s := Student{name: "charlie", age: 18}

fmt.Printf("%p\n", &s)

fmt.Println(s)

}


output:

0x2081c2000

0x2081c2040

name is charlie


as it shows, the argument passing to String() is actual a copy of the object, is that a waste of efficiency?

--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 2 of 6 | next ›
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 8, '14 at 1:53a
activeDec 8, '14 at 2:19p
posts6
users5
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase