go中指針的一種用法:
指針一般使用“*”,表示指向一個變量的值;取地址“&”表示去變量內存的地址。
這裏的testPointer函數返回值爲一個指針bool類型,但真正返回時是一個bool的地址。
在main中使用setup變量時,需要使用“*”進行取值操作
package main
import "fmt"
func testPointer() *bool {
setup := true
return &setup
}
func main() {
setup := testPointer()
fmt.Println("setup: ", setup)
fmt.Println("*setup: ", *setup)
}
Output:
setup: 0xc00004a080
*setup: true