Go語言學習筆記(十一)之指標
阿新 • • 發佈:2018-12-18
16 指標
變數和記憶體地址
1.每個變數都有記憶體地址,可以說通過變數來操作對應大小的記憶體,&符號可以獲取變數的地址
1: var a int322: a = 1003: fmt.Printf("%d\n", a)
4: fmt.Printf("%p\n", &a)
1: >>> 1002: 0xc00004a058
2.普通變數儲存的是對應型別的值,這些型別就叫值型別。指標型別儲存的是一個地址,又叫引用型別
1: var b int32 =1562: fmt.Printf("%d\n", b)
3: fmt.Printf("%p\n", &b)4: var c *int32 = &b5: var d *int //沒有初始化打印出來是nil
6: fmt.Printf("%p\n", c)
7: fmt.Printf("%v\n", d)
1: >>> 1562: 0xc00004a0583: 0xc00004a0584: <nil>
指標型別變數的預設值為nil,也就是空地址
1: package main2: import (
3: "fmt"
4: )5: func main() {6: a := 257: var b *int8: if b == nil {
9: fmt.Println("b is", b)
10: b = &a11: fmt.Println("b after initialization is", b)
12: }13: }
3.操作指標變數指向的地址裡面的值
通過*符號可以獲取指標變數指向的變數,還可以通過指標修改變數的值。
1: package main2: import (
3: "fmt"
4: )5: func main() {6: b := 2557: a := &b8: fmt.Println("address of b is", a)9: fmt.Println("value of b is", *a)
10: *b = 100011: fmt.Println(" new value of b is", *a)
12: }
1: >>> address of b is 0xc0000100902: value of b is 2553: new value of b is 1000
4.指標變數傳參
兩個例子
1: package main2: import (
3: "fmt"
4: )5: func change(val *int) {6: *val = 557: }8: func main() {9: a := 5810: fmt.Println("value of a before function call is", a)
11: b := &a12: change(b)13: fmt.Println("value of a after function call is", a)
14: }
1: package main2: import (
3: "fmt"
4: )5: func modify(arr *[3]int) {6: (*arr)[0] = 907: }8: func main() {9: a := [3]int{89,90,91}10: modify(&a)11: fmt.Println(a)12: }
關於值拷貝和引用拷貝,看下面的例子
1: package main2: import (
3: "fmt"
4: )5: func swap(a int, b int) {6: fmt.Printf("before a=%d b=%d\n", a, b)
7: a, b = b, a8: fmt.Printf("after a=%d b=%d\n", a, b)
9: }10: func main(){11: var a int = 1012: var b int = 2013: swap(a, b)14: fmt.Printf("in main_def a=%d b=%d\n", a, b)
15: }
列印結果,發現main函式裡面的ab的值沒有改變
1: >>> before a=10 b=202: after a=20 b=103: in main_def a=10 b=20
如果程式碼稍改下
1: package main2: import (
3: "fmt"
4: )5: func swap(a *int, b *int) {6: fmt.Printf("before a=%d b=%d\n", *a, *b)
7: *a, *b = *b, *a8: fmt.Printf("after a=%d b=%d\n", *a, *b)
9: }10: func main(){11: var a int = 1012: var b int = 2013: swap(&a, &b)14: fmt.Printf("in main_def a=%d b=%d\n", a, b)
15: }
結果就發生了改變
1: >>> before a=10 b=202: after a=20 b=103: in main_def a=20 b=10
總結:*符號跟指標型別,代表指向地址的變數。而&符號跟變數,代表變數的指標。