代码点读机 / Go / nil

nil Go · 内置功能

表示某些类型还没指向任何实际数据,像一把还没挂上钥匙的空钥匙扣。

怎么用

nil

小例子

var 指针 *int
var 切片 []int
fmt.Println(指针 == nil, 切片 == nil)

运行结果

输出 true true

提醒:nil 不能赋值给值类型(如 int、struct)

官方文档:https://go.dev/ref/spec#Predeclared_identifiers

在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← Go全部词条