Go supports pointers, allowing you to pass references to values and records within your program. |
|
package main
|
|
import "fmt"
|
|
We’ll show how pointers work in contrast to values with
2 functions: |
func zeroval(ival int) {
ival = 0
}
|
|
func zeroptr(iptr *int) {
*iptr = 0
}
|
func main() {
i := 1
fmt.Println("initial:", i)
|
|
zeroval(i)
fmt.Println("zeroval:", i)
|
|
The |
zeroptr(&i)
fmt.Println("zeroptr:", i)
|
Pointers can be printed too. |
fmt.Println("pointer:", &i)
}
|
|
$ go run pointers.go
initial: 1
zeroval: 1
zeroptr: 0
pointer: 0x42131100
|
Previous example: Recursion.
Next example: Structs.