Am Montag, 1. September 2014 00:58:46 UTC+2 schrieb JohnGB:
I have some code where I want to take in a pointer to a struct and create
a copy of that struct that I can change without changing the original

When I set up a simplified case, it works fine:

But when I modify the struct to be a compound struct, the same basic code
has a different behaviour: http://play.golang.org/p/QemQef4TBg

I'm at a complete loss as to why this is happening. Can anyone help me
out and explain why the behaviour changes?
The issue here has nothing to do with the second example being
a compound struct, but with slices behaving like reference types:
A slice like []name uses a backing array to store names. If you
copy a []name you will get a second slice which uses the _same_
backing array and thus shares the same content. Now your uppercasing
works on the same strings (same names) through different slices.

http://blog.golang.org/slices describes all this much better than
I could. It is worth a read.

You will have to add a copy method (or something that like) which
really does a deep copy of your slices.


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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
postedAug 31, '14 at 11:18p
activeAug 31, '14 at 11:18p

1 user in discussion

Volker Dobler: 1 post



site design / logo © 2023 Grokbase