當前位置:
首頁 >
Go 结构体的值传递和地址传递
發布時間:2024/9/30
34
豆豆
生活随笔
收集整理的這篇文章主要介紹了
Go 结构体的值传递和地址传递
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Go 結構體的值傳遞和地址傳遞
package mainimport "fmt"type student struct {id intname stringsex byteage intaddr string }func test01(s student) {s.id = 2fmt.Println("test s = ", s) }func test02(p *student ) {p.id = 3fmt.Println("p = ", *p) }func main() {s1 := student{id: 66, name: "后端碼匠", age: 18}fmt.Println("s1 = ", s1)test01(s1) //這里進行的是值傳遞,形參無法變更實參fmt.Println("s1 = ", s1)test02(&s1) //這里傳遞的是結構體的地址,函數里的操作會直接操作地址指向的結構體 }執行結果
s1 = {66 后端碼匠 0 18 } test s = {2 后端碼匠 0 18 } s1 = {66 后端碼匠 0 18 } p = {3 后端碼匠 0 18 }總結
以上是生活随笔為你收集整理的Go 结构体的值传递和地址传递的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Go go-metrics
- 下一篇: Go image: unknown fo