###### tags: `code` # Golang 拷貝技術整理 ## 淺拷貝 僅複製記憶體位置資訊,若修改則會連動修改 ## 深拷貝 複製真實值,共會有兩份記憶體位置,若修改則分別修改 ## 性質整理 ### 基本型別 string, int, float64, bool 單純指派視為值複製 ### 組合型別 slice, map ,struct 單純指派視為記憶體位置複製 ## 範例 ```go= a := []int{1, 2, 3} b := a fmt.Println(a) fmt.Println(b) b[0] = 777 fmt.Println(a) fmt.Println(b) ``` ```go= 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) ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up