15、指针 Pointers

97 阅读3分钟

本文为译文 Pointers

本文,我们将学习指针怎么在go里工作。同时去理解在go中指针和C、C++的区别。

This tutorial has the following sections.

  • What is a pointer?
  • Declaring pointers
  • Zero value of a pointer
  • Creating pointers using the new function
  • Dereferencing a pointer
  • Passing pointer to a function
  • Returning pointer from a function
  • Do not pass a pointer to an array as an argument to a function. Use slice instead.
  • Go does not support pointer arithmetic

什么是指针

指针是存储其他变量内存地址的变量(A pointer is a variable that stores the memory address of another variable.)

上面的插图中,变量b有个值是156,其被存储的内存地址是0x1040a124。变量a保存了b的地址,现在就说a指向b。

声明指针

*T是指针类型的变量。指向一个类型T的值。

package main

import (  
    "fmt"
)

func main() {  
    b := 255
    var a *int = &b
    fmt.Printf("Type of a is %T\n", a)
    fmt.Println("address of b is", a)
}

Type of a is *int  
address of b is 0x1040a124  

&操作符被用作获取一个变量的地址。第9行,我们分配b的地址给到变量a,a的类型为*int。我们现在说a指向b。

指针的零值

指针的零值是nil

package main

import (  
    "fmt"
)

func main() {  
    a := 25
    var b *int
    if b == nil {
        fmt.Println("b is", b)
        b = &a
        fmt.Println("b after initialization is", b)
    }
}

b is <nil>  
b after initialisation is 0x1040a124  

使用new function创建一个指针

go提供了一个便捷的new函数来创建一个指针。

new函数使用一个类型作为参数,返回一个指针,指向传递参数对应的零值。

package main

import (
	"fmt"
)

func main() {
	size := new(int)
	fmt.Printf("Size value is %d\n", size)
	fmt.Printf("type is %T\n", size)
	fmt.Printf("address is %v\n", size)

	*size = 85
	fmt.Println("New size value is", *size)
}


Size value is 824633827496
type is *int
address is 0xc00001a0a8
New size value is 85

指针取值

Dereferencing a pointer means accessing the value of the variable to which the pointer points. *a is the syntax to deference a.

package main  
import (  
    "fmt"
)

func main() {  
    b := 255
    a := &b
    fmt.Println("address of b is", a)
    fmt.Println("value of b is", *a)
}

package main

import (  
    "fmt"
)

func main() {  
    b := 255
    a := &b
    fmt.Println("address of b is", a)
    fmt.Println("value of b is", *a)
    *a++
    fmt.Println("new value of b is", b)
}

传递一个指针给函数

package main

import (  
    "fmt"
)

func change(val *int) {  
    *val = 55
}
func main() {  
    a := 58
    fmt.Println("value of a before function call is",a)
    b := &a
    change(b)
    fmt.Println("value of a after function call is", a)
}

value of a before function call is 58  
value of a after function call is 55 

从一个函数返回一个指针

把函数本地变量的指针值作为参数返回是完全合理的。

package main

import (  
    "fmt"
)

func hello() *int {  
    i := 5
    return &i
}
func main() {  
    d := hello()
    fmt.Println("Value of d", *d)
}

Value of d 5  

不要把数组的指针作为参数给函数,使用slice代替。

假设,我们想在函数内部对一个数组进行修改,同时调用者也能看见这个改变。一种方式是传递一个数组的指针作为参数。

package main

import (  
    "fmt"
)

func modify(arr *[3]int) {  
    (*arr)[0] = 90
}

func main() {  
    a := [3]int{89, 90, 91}
    modify(&a)
    fmt.Println(a)
}

This program outputs [90 90 91]。

a[x] is shorthand for (*a)[x]. So (*arr)[0] in the above program can be replaced by arr[0].

所以我们可以使用快捷语法这样写

package main

import (  
    "fmt"
)

func modify(arr *[3]int) {  
    arr[0] = 90
}

func main() {  
    a := [3]int{89, 90, 91}
    modify(&a)
    fmt.Println(a)
}

即使使用数组指针的形式是可以的,但是我们不这么用一般。一般我们用slice

package main

import (  
    "fmt"
)

func modify(sls []int) {  
    sls[0] = 90
}

func main() {  
    a := [3]int{89, 90, 91}
    modify(a[:])
    fmt.Println(a)
}

记得slice是引用类型

go不支持指针算术

package main

func main() {  
    b := [...]int{109, 110, 111}
    p := &b
    p++
}

The above program will throw compilation errormain.go:6: invalid operation: p++ (non-numeric type *[3]int)