On Tue, Mar 6, 2018 at 2:58 PM, Bharath Ram <bharathram...@gmail.com> wrote: > In that case, for a nested struct from top to bottom, all the values are > copies as values, correct? i.e, string, int, float, array etc will be copied > by value & map, slice, channel etc will be copied as pointers if they are > initialized? > > https://play.golang.org/p/NzH8LVyQ0rp code works as I wanted. Generally, > what practice should I follow? If I want a shared variable, should I have to > keep all the variables as pointers like in the example > (https://play.golang.org/p/NzH8LVyQ0rp) or like the way I showed earlier?
It depends on the case. What exactly do you want to do? I find dealing with the semantics of copies of structures harder to comprehend, so I try to avoid it when the structure has reference types, but sometimes it is really useful. For instance, if the structure will be used as read-only and has a reasonable size, a copy of it is just fine. In my opinion, the only general rule is that you have to know how the data is organized and that you know what you're doing. > > On Tuesday, March 6, 2018 at 3:40:47 PM UTC-5, Burak Serdar wrote: >> >> On Tue, Mar 6, 2018 at 1:23 PM, <bharat...@gmail.com> wrote: >> > go version 1.10 >> > >> > >> > Operating system and processor architecture >> > >> > GOHOSTARCH="amd64" >> > GOHOSTOS="darwin" >> > However, this happens in play.golang.org env as well. >> > >> > >> > Checkout the code. >> > >> > https://play.golang.org/p/v6u7R_nbGRp >> >> doc1 := Document{} >> // Here, doc1.Data is nil >> >> ds := DataStore{Doc: doc1} >> // Here, ds.Doc has a copy of doc1, and ds.Doc.Data is nil >> >> ds.Doc.Data = DocData{} >> // Here, ds.Doc.Data is initialized, but not doc1.Data, because >> ds.Doc is a copy of doc1 >> >> >> On the other hand: >> >> doc2 := Document{Data: DocData{}} >> // Here, doc2.Data is initialized to an empty map >> ds2 := DataStore{Doc: doc2} >> // And here, ds2.Doc is a copy of doc2, which contains a reference to >> that empty map. The underlying maps are shared. >> >> >> >> > >> > >> > In the example, if the struct (Document) is not initialized at first, >> > there >> > is no place holder for future values pointing to same object. Is this >> > intended? >> > >> > -- >> > 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...@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+unsubscr...@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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.