c := getX() c.x = 1 vs
getX().x = 1 On Wed, Nov 20, 2019 at 2:32 PM Robert Engels <reng...@ix.netcom.com> wrote: > I must of misread something, because this code works fine: > > package main > > import "fmt" > > type X struct { > x int > } > > func getX() X { > var x X > return x > } > > func main() { > c := getX() > c.x = 1 > fmt.Println(c) > } > > and is what I would expect to happen. Why is this not the same behavior in > the other code? > > > > > > -----Original Message----- > >From: Robert Engels <reng...@ix.netcom.com> > >Sent: Nov 20, 2019 2:25 PM > >To: Ian Lance Taylor <i...@golang.org>, Orson Cart < > objectivedynam...@gmail.com> > >Cc: golang-nuts <golang-nuts@googlegroups.com> > >Subject: Re: [go-nuts] Difference with function returning struct and > function returning pointer to struct > > > > > >To be honest, I find this very confusing, especially the statement > "Changing a field in that copy won't change anything." because even if I > have a copy, I should be able to change the fields, and the changes would > be there for the scope, and if I returned the copy, whether by address or > copy, the changes would be retained. > > > >-----Original Message----- > >>From: Ian Lance Taylor <i...@golang.org> > >>Sent: Nov 20, 2019 11:55 AM > >>To: Orson Cart <objectivedynam...@gmail.com> > >>Cc: golang-nuts <golang-nuts@googlegroups.com> > >>Subject: Re: [go-nuts] Difference with function returning struct and > function returning pointer to struct > >> > >>On Wed, Nov 20, 2019 at 9:41 AM Orson Cart <objectivedynam...@gmail.com> > wrote: > >>> > >>> I'm a newbie to Go having used C and C++ in the past and I'm puzzled > about something I've just seen. > >>> > >>> In the following code getEmployee returns a pointer to a struct. The > return value is then dereferenced. This code compiles and runs fine: > >>> > >>> package main > >>> > >>> type employee struct { > >>> ID int > >>> name string > >>> } > >>> > >>> var dilbert employee > >>> > >>> func main() { > >>> > >>> getEmployee().name = "dilbert" > >>> } > >>> > >>> func getEmployee() *employee { > >>> return &dilbert > >>> } > >>> > >>> However if I modify getEmployee as follows, I get a compilation error: > >>> > >>> func getEmployee() employee { > >>> return dilbert > >>> } > >>> > >>> The error is: cannot assign to getEmployee().name > >>> > >>> I assume that it revolves around "assignability" but I'm struggling to > understand why. > >>> > >>> Can anyone tell me why this is? > >> > >>There are two kinds of answers. > >> > >>One is that the spec only permits setting fields of a struct that is > >>addressable, and the result of a function call is not addressable. > >>See https://golang.org/ref/spec#Assignments and > >>https://golang.org/ref/spec#Address_operators . > >> > >>The other kind of answer is that if a function returns a pointer to a > >>struct, a value of type *employee, then it's normal for that something > >>else to have a copy of that pointer, perhaps some data structure or > >>global variable. In your simple example that is the global variable > >>"dilbert". So it makes sense to permit setting a field of the result > >>of calling getEmployee(), a value of type *employee, as setting that > >>field will change the global variable "dilbert". But when > >>getEmployee() does not return a pointer, but just returns a value of > >>type "employee", then that is a copy of the value in the global > >>variable. Changing a field in that copy won't change anything. The > >>assignment will be made, and then the result will be discarded. Since > >>that operation is useless, the language does not permit it. > >> > >>Ian > >> > >>-- > >>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+unsubscr...@googlegroups.com. > >>To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/CAOyqgcVz5oaDa98bXMKS5mKW-ya55dewcrUtmQZcy-v0-YUicg%40mail.gmail.com > . > > > >-- > >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+unsubscr...@googlegroups.com. > >To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/842040269.1664.1574281551187%40wamui-scooby.atl.sa.earthlink.net > . > > -- > 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+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/2134227176.1691.1574281911663%40wamui-scooby.atl.sa.earthlink.net > . > -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CABn4WMX0KHYjMVdV-HKCwwW3F-CWfJzToOM%2BkG_JfqDe%3DKjBxA%40mail.gmail.com.