This is a creation in Article, where the information may have evolved or changed.
The renewal day asks me today: Golang inside if do structure of deep copy? He said his Excelize library was reported by netizens a bug:
Change the data on the Sheet2 page, the data on the Sheet1 changes together, and finally he locates the problem because he made a shallow copy of two sheet structures. Because the sheet corresponds to the particular structure, it must be a pointer type. Oh, yes... So it's a pit, Golang. How do you make a deep copy of a pointer variable in a super-invincible complex structure? Give an example:
type KDeepCopy struct { A map[string]string B []string C struct { D [][]string E map[interface{}]F G string K func() }}type F struct { K map[int]*string}
var k *KDeepCopy = &KDeepCopy{....} , var h *KDeepCopy。
At this point, we want to copy all the memory values of the K variable to the H variable, that is, K and H point to different memory regions. In C + + we can use the memcpy function (or a similar function) to do this. But Golang officially did not provide such API functions to us directly to manipulate memory. Here's a solution (continued God provided), I wrote a simple demo code verification. The demo gives JSON and gob two ways to solve the two kinds of codec scheme which better, their own evaluation.
Package Mainimport ("bytes" "Encoding/gob" "Encoding/json" "FMT") type gdeepcopy struct {A Map[string]stri Ng B []string d int}type deepcopy struct {A map[string]string B []string C cc}type Cc struct {D map[in T]*string}func Main () {var cc cc = cc{d:make (map[int]*string, 0), var dc deepcopy = deepcopy{ A:make (map[string]string, 0), B: []string{' B ', ' C ', ' d '}, C:CC,} DV: = deepcopy{} dv = DC FMT . Printf ("DC:%p\n", &DC) fmt. Printf ("DV:%p\n", &DV) dv. B = []string{' f ', ' G ', ' d '} fmt. Println (' DC: ', DC) fmt. Println (' DV: ', DV) fmt. Println (' ============================= ') var df *deepcopy = &deepcopy{a:make (map[string]string, 0), B: []string{' B ', ' C ', ' d '}, C:CC,} DN: = &deepcopy{} DN = df fmt. Printf ("DN:%p\n", DN) fmt. Printf ("DF:%p\n", DF) DN. B = []string{' f ', ' G ', ' d '} fmt. Println (' DF: ', DF) fmt. Println (' dn: ', DN) Fmt. Println (' ============================= ') var buf bytes. Buffer dg: = &deepcopy{} json. Newencoder (&BUF). Encode (*DF) JSON. Newdecoder (bytes. Newbuffer (BUF. Bytes ())). Decode (DG) DG. B = []string{' h ', ' J ', ' K '} fmt. Printf ("DF:%p\n", DF) fmt. PRINTF ("DG:%p\n", DG) FMT. Println (' DF: ', DF) fmt. Println (' DG: ', DG) FMT. Println (' ============================= ') DJ: = &gdeepcopy{} gob. Newencoder (&BUF). Encode (*DF) gob. Newdecoder (bytes. Newbuffer (BUF. Bytes ())). Decode (DJ) DJ. B = []string{' q ', ' W ', ' E '} fmt. Printf ("DF:%p\n", DF) fmt. Printf ("DJ:%p\n", DJ) FMT. Println (' DF: ', DF) fmt. Println (' DJ: ', DJ) FMT. Println (' ============================= ')}
Yes, you can serialize the struct to a buffer, and then deserialize the byte () data inside the buffer into the execution object. Do you feel good trouble? If you have any good plans, please post them in the comments section.