Golang语言指针

从指针获取指针指向的值

名词:指针类型、指针地址、&、*

//  ptr-pointer (既指针)得缩写

var house string = "777"

ptr := &house // &取地址操作符,对字符串取地址,将指针保存到变量ptr中

fmt.Printf("ptr type: %T\n", ptr)    // 指针类型为 *string
fmt.Printf("ptr address: %p\n", ptr) // 指针地址,指向对象在内存中存放的位置。每次运行都会发生变化。

value := *ptr                         // *取值操作符,对指针进行取值操作
fmt.Printf("value type: %T\n", value) // 打印为string
fmt.Printf("value address: %s\n", value)

使用指针修改值

使用指针变量获取命令行的输入信息

var mode = flag.String("mode", "", "process mode")
flag.Parse()
fmt.Println(*mode)

go run src/day_02/Bool类型/bool.go --mode=7

创建指针的另一种方法 new()函数

//用法如下:
str2 := new(string)
*str2 = "加油加油"
fmt.Printf("%s\n", *str2) // 加油加油

参考地址:http://c.biancheng.net/view/21.html