标签:go *int32类型赋值
直接上代码:package main
import "fmt"
func main() {
var num *int32
var i int32 = 5
// 针对*int32的,先创建一个i类型,然后取i的地址,赋值给*int32
num = &i
fmt.Println("=====>:\t", num)
fmt.Println("=====>:\t", *num)
}
运行结果:
=====>: 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)
}
运行结果:
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的地址
标签:go *int32类型赋值
原文地址:http://blog.51cto.com/xingej/2117313