funcmain() { num :=1 outside :=&num update(outside) fmt.Printf("outside value = %v\n", outside) fmt.Printf("outside pointed value = %v\n", *outside) fmt.Printf("outside address = %p\n", &outside) }
结果为
1 2 3 4 5 6
inside value =0xc000100010 inside pointed value =2 inside address =0xc000102020 outside value =0xc000100010 outside pointed value =2 outside address =0xc000102018
func update(inside []int) { inside[2] = 4 fmt.Printf("inside value = %v\n", inside) fmt.Printf("inside address = %p\n", &inside) fmt.Printf("inside first element address = %p\n", &inside[0]) }
func main() { outside := []int{1, 2, 3} update(outside) fmt.Printf("outside value = %v\n", outside) fmt.Printf("outside address = %p\n", &outside) fmt.Printf("outside first element address = %p\n", &outside[0]) }
结果为
1 2 3 4 5 6
inside value = [124] inside address = 0xc00000c0c0 inside first element address = 0xc000014020 outside value = [124] outside address = 0xc00000c0a0 outside first element address = 0xc000014020
func update(inside []int) { inside = append(inside, 4) fmt.Printf("inside value = %v\n", inside) fmt.Printf("inside address = %p\n", &inside) fmt.Printf("inside first element address = %p\n", &inside[0]) }
func main() { outside := []int{1, 2, 3} update(outside) fmt.Printf("outside value = %v\n", outside) fmt.Printf("outside address = %p\n", &outside) fmt.Printf("outside first element address = %p\n", &outside[0]) }
结果为
1 2 3 4 5 6
inside value = [1234] inside address = 0xc0000ae060 inside first element address = 0xc0000b2030 outside value = [123] outside address = 0xc0000ae040 outside first element address = 0xc0000be000
func update(inside Person) { inside.Card.Id = "456" inside.Age = 20 fmt.Printf("inside Card value = %v[address=%p]\n", inside.Card, inside.Card) fmt.Printf("inside Age value = %v\n", inside.Age) fmt.Printf("inside address = %p\n", &inside) }
func main() { outside := Person { Card: &IdCard{ Id: "123", Time: "2020-01-01", }, Age: 18, } update(outside) fmt.Printf("outside Card value = %v[address=%p]\n", outside.Card, outside.Card) fmt.Printf("outside Age value = %v\n", outside.Age) fmt.Printf("outside address = %p\n", &outside) }
运行结果为
1 2 3 4 5 6
inside Card value = &{4562020-01-01}[address=0xc00009e000] inside Age value = 20 inside address = 0xc00009c050 outside Card value = &{4562020-01-01}[address=0xc00009e000] outside Age value = 18 outside address = 0xc00009c040