Go by Example - Pointers

翻译自 https://gobyexample.com/

Go by Example

Go is an open source programming language designed for building simple, fast, and reliable software.

Go by Example is a hands-on introduction to Go using annotated example programs. Check out the first example or browse the full list below.

Go by Example: Pointers

golang支持指针,允许在程序中传递值和记录的引用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package main

import "fmt"

// 接收一个int类型参数。
// 对ival的赋值不会影响外部变量的值。
func zeroval(ival int) {
ival = 0
}

// 接收一个int指针类型的参数。
// 对*iptr的赋值操作会直接修改到iptr指针所指向内存的地址。
func zeroptr(iptr *int) {
*iptr = 0
}

func main() {
i := 1
fmt.Println("initial:", i)

// 传值。
zeroval(i)
fmt.Println("zeroval:", i)

// 传引用。
zeroptr(&i)
fmt.Println("zeroptr:", i)

// 打印指针指向的内存地址。
fmt.Println("pointer:", &i)
}
1
2
3
4
5
$ go run pointers.go
initial: 1
zeroval: 1
zeroptr: 0
pointer: 0x42131100

原文链接:Go by Example: Pointers