package main import "fmt" type student struct { id int name string sex byte age int addr string } func test01(s student) { s.id = 2 fmt.Println("test s = ", s) } func test02(p *student ) { p.id = 3 fmt.Println("p = ", *p) } func main() { s1 := student{id: 1, name: "baylorqu", age: 18} fmt.Println("s1 = ", s1) test01(s1) //这里进行的是值传递,形参无法变更实参 fmt.Println("s1 = ", s1) test02(&s1) //这里传递的是结构体的地址,函数里的操作会直接操作地址指向的结构体 }
执行结果为
s1 = {1 baylorqu 0 18 } test s = {2 baylorqu 0 18 } s1 = {1 baylorqu 0 18 } p = {3 baylorqu 0 18 }