code
僅複製記憶體位置資訊,若修改則會連動修改
複製真實值,共會有兩份記憶體位置,若修改則分別修改
string, int, float64, bool 單純指派視為值複製
slice, map ,struct 單純指派視為記憶體位置複製
a := []int{1, 2, 3}
b := a
fmt.Println(a)
fmt.Println(b)
b[0] = 777
fmt.Println(a)
fmt.Println(b)
a := []int{1, 2, 3}
b := make([]int, 3)
copy(b, a)
fmt.Println(a)
fmt.Println(b)
b[0] = 777
fmt.Println(a)
fmt.Println(b)
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up