Go语言之指针
point_demo.go文件
package point_demo
import "fmt"
func TestPoint() {
var count int = 20
var countPoint *int
var countPoint1 *int
countPoint = &count
fmt.Printf("count 变量的地址:%x \n", &count) //count 变量的地址:c00000a0a0
fmt.Printf("countPoint 变量存储的地址:%x \n", countPoint) //countPoint 变量存储的地址:c00000a0a0
fmt.Printf("countPoint 指针指向地址的值:%d \n", *countPoint) //countPoint 指针指向地址的值:20
if countPoint1 == nil {
fmt.Printf("countPoint1 变量存储的地址:%x \n", countPoint1) //countPoint1 变量存储的地址:0
}
}
main.go文件
package main
import "./point_demo"
func main() {
point_demo.TestPoint()
}
point_demo.go文件
package point_demo
import "fmt"
func TestPointArr() {
//指针数组
a,b := 1,2
pointArr :=[...]*int{&a, &b}
fmt.Println("指针数组pointArr:", pointArr)//指针数组pointArr: [0xc0000a2058 0xc0000a2070]
//数组指针
arr := [...]int{3,4,5}
arrPoint := &arr
fmt.Println("数组指针arrPoint:", arrPoint)//数组指针arrPoint: &[3 4 5]
}
main.go文件
package main
import "./point_demo"
func main() {
point_demo.TestPointArr()
}