直接上代碼:
網(wǎng)站建設(shè)公司,為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁(yè)設(shè)計(jì)及定制網(wǎng)站建設(shè)服務(wù),專注于企業(yè)網(wǎng)站設(shè)計(jì),高端網(wǎng)頁(yè)制作,對(duì)成都履帶攪拌車等多個(gè)行業(yè)擁有豐富的網(wǎng)站建設(shè)經(jīng)驗(yàn)的網(wǎng)站建設(shè)公司。專業(yè)網(wǎng)站設(shè)計(jì),網(wǎng)站優(yōu)化推廣哪家好,專業(yè)成都網(wǎng)站推廣優(yōu)化,H5建站,響應(yīng)式網(wǎng)站。
package main
import "fmt"
func main() {
var num *int32
var i int32 = 5
// 針對(duì)*int32的,先創(chuàng)建一個(gè)i類型,然后取i的地址,賦值給*int32
num = &i
fmt.Println("=====>:\t", num)
fmt.Println("=====>:\t", *num)
}
運(yùn)行結(jié)果:
=====>: 0xc042064080
=====>: 5
Process finished with exit code 0
下面的形式是不可以的:
package main
import "fmt"
func main() {
var num *int32
*num = 5
fmt.Println("=====>:\t", num)
fmt.Println("=====>:\t", *num)
}
運(yùn)行結(jié)果:
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x1 addr=0x0 pc=0x48b4ea]
goroutine 1 [running]:
main.main()
E:/Program/go2/goPath/src/xingej-go/xingej-go/xingej-go666/string/test.go:7 +0x2a
Process finished with exit code 2
原因?
可能是取不到常量5的地址