table of Contents
And pointer type pointer address 0x00
Variable corresponding to a variable holds a value corresponding to the type of memory, a pointer value is the address of another variable, the variable pointer may point to a memory address of any value.
Address Symbol taken &
before adding pointer type *
number, the content may be acquired pointer points, which is a type of change, a reference to a pointer using a value called indirect reference.
prt := &v // v 的类型为T
v
It is taken to represent the variable address, prt
the received v
address, prt
the type *T
, known as T
pointer type.
0x01 Get pointer value from a pointer
Using the &
following variables take on the address to obtain the pointer variable, you may use a pointer *
to obtain the value of the pointer number is called the pointer value.
temp := "test content"
prt := &temp
fmt.Println(*prt) // 打印 test content
Variables, characteristics and the relationship between the address pointers, pointer variable, taking the address, the value is as follows:
Taken to address variable (&) operation can be obtained pointer variable this variable.
Value of the pointer variable is a pointer address.
Pointer variable value (*) value of the original variable operation can be obtained pointer variable points.
A pointer modification value 0x02
Use *
of the modified value and pointer operations
x, y := 1, 2
fmt.Println(x, y) // 1 2
j, k := &x, &y
*j, *k = *k, *j
fmt.Println(x, y) // 2 1
*
Fundamental operator is operating variable pointer. When the operation value in the right, is to take the point value of the variable; value when operating in the left, the value is set to variable points.
If you just pointer references are exchanged, then the values and addresses are no effect on the variable is referenced, affecting only a pointer applications.
x, y := 1, 2
fmt.Println(x, y) // 1 2
j, k := &x, &y
j, k = k, j
fmt.Println(x, y) // 1 2
fmt.Printf("x : %p, y : %p \n", &x, &y) // x : 0xc00001a088, y : 0xc00001a090
fmt.Printf("j : %p, y : %p \n", j, k) //j : 0xc00001a090, y : 0xc00001a088
x, y values and the address does not influence the pointer j, k after switching, j, k value exchanged.
0x03 return function local variables
In the Go language, the function returns the address of a local variable is safe, for example, the following code to create a local variable v When you call the function f is still valid after the local variable address is returned, because the pointer still refer to this variable p
var p = f()
func f() *int {
v := 1
return &v
}
0x04 use new () to create a pointer
temp := new(int)
*temp = 123
fmt.Println(*temp) // 123
fmt.Println(temp) // 0xaabb
0x05 flag art packet pointer
Pointer is a key technology in the standard library package flag, which is used to implement the command line parsing flag.
example:
package main
import (
"flag"
"fmt"
"strings"
)
var n = flag.Bool("n", true, "print test")
var sep = flag.String("s", " ", "separator")
var test = flag.String("test", " ", "测试")
func main() {
flag.Parse()
fmt.Println(strings.Join(flag.Args(), *sep))
if *n {
fmt.Println(*test)
}
}
run
$ go run main.go --help
Usage of /var/exe/main:
-n print test
-s string
separator (default " ")
-test string
测试 (default " ")
exit status 2
$ go run main.go -s "+" --test 测试文本 a bc def 123
a+bc+def+123
$ go run main.go -s "+" -n --test 测试文本 a bc def 123
a+bc+def+123
测试文本