This is a creation in
Article, where the information may have evolved or changed.
- First come to the conclusion
All of Golang's built-in types are passed as function arguments (without passing a reference), and it is important to note that arrays, slice, and maps are also passed as function arguments, but if modifications are made to elements within the structure , the original data is modified. If you assign it as a whole, the original data is not modified, which is equivalent to copying a new temporary variable. To modify the original data in any situation, you can implement it by passing pointer parameters.
1 Package Main2 3 Import (4 "bytes"5 "FMT"6 "Strings"7 )8 9Func F_1 (Aint) {TenA =2 One } A -Func F_1_1 (A *int) { -*a =2 the } - -Func F_2 (Sstring) { -s ="CBA" + } - +Func F_2_1 (S *string) { A*s ="CBA" at } - -Func f_3 (v []string) { -v[0] ="haha" - } - inFunc F_3_1 (v []string) { -v =Nil to } + -Func f_3_2 (v *[]string) { the*v =Nil * } $ Panax NotoginsengFunc F_4 (M map[int]int) { -m[1] =3 them[3] =1 + } A theFunc F_4_1 (M map[int]int) { +m =Nil - } $ $Func F_4_2 (M *map[int]int) { -*m =Nil - } the -Func f_5 (b []byte) {Wuyib[0] =0x40 the } - WuFunc F_5_1 (b []byte) { -b = bytes. Replace (b, []byte("1"), []byte("a"), -1) About } $ -Func F_5_2 (b *[]byte) { -*b = bytes. Replace (*b, []byte("1"), []byte("a"), -1) - } A +Type whystruct { thes []string - } $ theFunc (ss why) Setv (s []string) { theSS.S =s the } the -Func (SS *why) SETP (s []string) { inSS.S =s the } the AboutFunc (ss why) String ()string { the returnStrings. Join (SS.S,",") the } the + Func Main () { -A: =1 theS: ="ABC"BayiV: = []string{"SD","AA"} theM: = map[int]int{1:1,2:2,3:3} the F_1 (a) - f_2 (s) - F_3 (v) the F_4 (m) theFmt. Printf ("%d,%s,%v,%v\n", A, S, V, m) the F_3_1 (v) the F_4_1 (m) -Fmt. Printf ("%d,%s,%v,%v\n", A, S, V, m) theF_1_1 (&a) theF_2_1 (&s) theF_3_2 (&v)94F_4_2 (&m) theFmt. Printf ("%d,%s,%v,%v\n", A, S, V, m) theB: = []byte("12145178") the F_5 (b)98Fmt. Printf ("%s\n", B) About F_5_1 (b) -Fmt. Printf ("%s\n", B)101F_5_2 (&b)102Fmt. Printf ("%s\n", B)103SS: = &why{}104Ss. Setv ([]string{"ABC","EFG"}) the FMT. PRINTLN (ss)106Ss. SETP ([]string{"ABC","EFG"})107 FMT. PRINTLN (ss)108}
The output is as follows
1 1, Abc,[haha aa],map[1:3 2:2 3:1] Slice and map value passing can modify the original data, but the basic data type does not2 1, Abc,[haha aa],map[1:3 2:2 3:1] The overall assignment does not modify the original data, it is worth noting that the map is unordered3 2, cba,[],map[] passing pointers always modifies the original data4@2145178Ibid .5@2145178Use bytes. Replace is actually assigned, so the original data is not modified6 @2a45a78 Use the pointer to pass the argument.7 The member function of a class is defined as a pass-through value and does not modify the original data (the original data is empty)8 the member function of the ABC,EFG class is defined as the way the pointer is passed, and the original member variable can be modified9 TenSuccess: Process exit code0.
Do you get it now?