Go 语言的 nil 能比较自己吗

nil 是什么?

可以看看官方的描述,在 buildin/buildin.go 里:

// nil is a predeclared identifier representing the zero value for a
// pointer, channel, func, interface, map, or slice type.
var nil Type // Type must be a pointer, channel, func, interface, map, or slice type

nil 是一个预先声明的标识符,代表指针、通道、函数、接口、哈希表或切片的零值。

OK,回到问题,nil 能比较自己吗?

package main
import "fmt"
func main() {
fmt.Println(nil == nil)
}
// # command-line-arguments
// ./main.go:6:18: invalid operation: nil == nil (operator == not defined on nil)

没有具体的类型,编译器懵逼了说没办法比较。

slice 什么时候是 nil ?

看看下面的代码:

var slice = []string{}
fmt.Println(slice == nil) // false
type GoSlice struct {
ptr unsafe.Pointer
len int
cap int
}
s := (*GoSlice)(unsafe.Pointer(&a))
s.ptr = nil
fmt.Println(s == nil) // true

神奇,切片底层指针为 nil 的时候,就是一个 nil 切片。map、channel、func 也是类似的。

nil 和 interface 一起使用的坑

继续看代码:

type A interface {}
type a struct {}
func Foo() A {
var data *a // nil
return data
}
fmt.Println(Foo() == nil) // false

嗯,坑得体无完肤。很反人类!接口为 nil,必须是类型和值同时为 nil,才能等于 nil,无聊的知识又增加了:)


文章来源于本人博客,发布于 2019-06-16,原文链接:https://imlht.com/archives/190/

posted @   仁扬  阅读(31)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示