1 基本数据类型和struct声明时都有初始值,不能和nil进行比较
var a int
fmt.Printf("-------&q=%p type:%T v:%v\n", &a, a, a)
var float1 float32
fmt.Printf("-------&q=%p type:%T v:%v\n", &float1, float1, float1)
var complex1 complex128
fmt.Printf("-------&q=%p type:%T v:%v\n", &complex1, complex1, complex1)
var b bool
fmt.Printf("-------&q=%p type:%T v:%v\n", &b, b, b)
var str string
fmt.Printf("-------&q=%p type:%T v:%v\n", &str, str, str)
type struct1 struct {
iii int
bbb int
inter interface{}
}
var struc struct1
fmt.Printf("-------&q=%p type:%T v:%v\n", &struc, struc, struc)
输出:
-------&q=0xc000060058 type:int v:0
-------&q=0xc000060090 type:float32 v:0
-------&q=0xc0000600a0 type:complex128 v:(0+0i)
-------&q=0xc000060094 type:bool v:false
-------&q=0xc00004c1f0 type:string v:
-------&q=0xc000058440 type:main.struct1 v:{0 0 <nil>}
2 常量值是没有地址的
const const1 = "hello"
fmt.Printf("-------&q=%p type:%T v:%v\n", const1, const1, const1)
输出:
&q=%!p(string=hello) type:string v:hello
3 指针,函数值,slice,map,chan,interface声明时都是默认为nil
var strPointer *string
fmt.Printf("-------&q=%p type:%T v:%v is nil:%v\n", &strPointer, strPointer, strPointer, strPointer == nil)
var f func(int) int
fmt.Printf("-------&q=%p type:%T v:%v is nil:%v\n", &f, f, f, f == nil)
var s []int
fmt.Printf("-------&q=%p type:%T v:%v is nil:%v\n", &s, s, s, s == nil)
var m map[string]string
fmt.Printf("-------&q=%p type:%T v:%v is nil:%v\n", &m, m, m, m == nil)
var c chan int
fmt.Printf("-------&q=%p type:%T v:%v is nil:%v\n", &c, c, c, c == nil)
var i interface{}
fmt.Printf("-------&q=%p type:%T v:%v is nil:%v\n", &i, i, i, i == nil)
输出:
-------&q=0xc00008c020 type:*string v:<nil> is nil:true
-------&q=0xc00008c020 type:func(int) int v:<nil> is nil:true
-------&q=0xc0000564a0 type:[]int v:[] is nil:true
-------&q=0xc00008c028 type:map[string]string v:map[] is nil:true
-------&q=0xc00008c030 type:chan int v:<nil> is nil:true
-------&q=0xc00004c200 type:<nil> v:<nil> is nil:true