1.新声明的string 变量 都为nil ,在数值上是相等的,但是在地址上是不等的
package main
import "fmt"
func main() {
var a string
var b string
fmt.Println(a==b,&a==&b)
fmt.Printf("%T,%T,%p,%p\n",a,b,&a,&b)
输出:
true false
string,string,0xc00008c1e0,0xc00008c1f0
2.指针,函数值,slice,map,chan,interface声明时都是默认为nil
package main
import "fmt"
func main() {
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=0xc0000ce020 type:*string v:<nil> is nil:true
-------&q=0xc0000ce028 type:func(int) int v:<nil> is nil:true
-------&q=0xc00009a460 type:[]int v:[] is nil:true
-------&q=0xc0000ce030 type:map[string]string v:map[] is nil:true
-------&q=0xc0000ce038 type:chan int v:<nil> is nil:true
-------&q=0xc00008c200 type:<nil> v:<nil> is nil:true
3.不同类型的nil,不能进行比较(包括等于)
4.基本数据类型和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>}
5.常量是没有地址的
const const1 = "hello"
fmt.Printf("-------&q=%p type:%T v:%v\n", const1, const1, const1)
输出:
// 输出地址,就是输出常量的值
&q=%!p(string=hello) type:string v:hello