The first contact with Golang language, the understanding of Go is temporarily more limited.
The go pointer is simply learned here, and it feels similar to the C pointer you learned before.
The address character of the Go language is &, and the memory address of the corresponding variable is returned by using it before a variable is placed.
adding * to the address is the operation to take the corresponding value of the address.
The practice code is as follows:
package mainimport "fmt"func main() { var a int = 10 var b string = "hello world" var c bool = true var d float32 = 0.002 var e float64 = 0.00000003 fmt.Printf("a变量的地址: %x,a的值为:%d\n", &a,*&a) fmt.Printf("变量的地址: %x\n", &b) fmt.Printf("变量的地址: %x\n", &c) fmt.Printf("变量的地址: %x\n", &d) fmt.Printf("变量的地址: %x\n", &e) var x int = 100 var y int = 10 swap(&x,&y) fmt.Printf("x=%d,y=%d",x,y)} /*利用指针交互xy值 */func swap(x *int, y *int) { var temp int temp = *x /* 保存 x 地址的值 */ *x = *y /* 将 y 赋值给 x */ *y = temp /* 将 temp 赋值给 y */ }
Log:
1530808982516.jpg
At present, the understanding of Go language is very limited, I hope I can also step by step learning to accumulate into the lakes and rivers in the great god it! Ha ha