在 Go 中如何让结构体不可比较?

最近我在使用 Go 官方出品的结构化日志包 slog 时,看到 slog.Value 源码中有一个比较好玩的小 Tips,可以限制两个结构体之间的相等性比较,本文就来跟大家分享下。

在 Go 中结构体可以比较吗?

在 Go 中结构体可以比较吗?这其实是我曾经面试过的一个问题,我们来做一个实验:

定义如下结构体:

type Normal struct {
 a string
 B int
}

使用这个结构体分别声明 3 个变量 n1n2n3,然后进行比较:

n1 := Normal{
 a: "a",
 B: 10,
}
n2 := Normal{
 a: "a",
 B: 10,
}
n3 := Normal{
 a: "b",
 B: 20,
}

fmt.Println(n1 == n2)
fmt.Println(n1 == n3)

执行示例代码,输出结果如下:

$ go run main.go
true
false

可见 Normal 结构体是可以比较的。

如何让结构体不可比较?

那么所有结构体都可以比较吗?显然不是,如果都可以比较,那么 reflect.DeepEqual() 就没有存在的必要了。

定义如下结构体:

type NoCompare struct {
 a string
 B map[string]int
}

使用这个结构体分别声明 2 个变量 n1n2,然后进行比较:

n1 := NoCompare{
 a: "a",
 B: map[string]int{
  "a": 10,
 },
}
n2 := NoCompare{
 a: "a",
 B: map[string]int{
  "a": 10,
 },
}

fmt.Println(n1 == n2)

执行示例代码,输出结果如下:

$ go run main.go
./main.go:59:15: invalid operation: n1 == n2 (struct containing map[string]int cannot be compared)

这里程序直接报错了,并提示结构体包含了 map[string]int 类型字段,不可比较。

所以小结一下:

结构体是否可以比较,不取决于字段是否可导出,而是取决于其是否包含不可比较字段。

如果全部字段都是可比较的,那么这个结构体就是可比较的。

如果其中有一个字段不可比较,那么这个结构体就是不可比较的。

不过虽然我们不可以使用 ==n1n2 进行比较,但我们可以使用 reflect.DeepEqual() 对二者进行比较:

fmt.Println(reflect.DeepEqual(n1, n2))

执行示例代码,输出结果如下:

$ go run main.go
true

更优雅的做法

最近我在使用 Go 官方出品的结构化日志包 slog 时,看到 slog.Value 源码:

// A Value can represent any Go value, but unlike type any,
// it can represent most small values without an allocation.
// The zero Value corresponds to nil.
type Value struct {
 _ [0]func() // disallow ==
 // num holds the value for Kinds Int64, Uint64, Float64, Bool and Duration,
 // the string length for KindString, and nanoseconds since the epoch for KindTime.
 num uint64
 // If any is of type Kind, then the value is in num as described above.
 // If any is of type *time.Location, then the Kind is Time and time.Time value
 // can be constructed from the Unix nanos in num and the location (monotonic time
 // is not preserved).
 // If any is of type stringptr, then the Kind is String and the string value
 // consists of the length in num and the pointer in any.
 // Otherwise, the Kind is Any and any is the value.
 // (This implies that Attrs cannot store values of type Kind, *time.Location
 // or stringptr.)
 any any
}

可以发现,这里有一个匿名字段 _ [0]func(),并且注释写着 // disallow ==

_ [0]func() 的目的显然是为了禁止比较。

我们来实验一下,_ [0]func() 是否能够实现禁止结构体相等性比较:

v1 := Value{
 num: 1,
 any: 2,
}
v2 := Value{
 num: 1,
 any: 2,
}

fmt.Println(v1 == v2)

执行示例代码,输出结果如下:

$ go run main.go
./main.go:109:15: invalid operation: v1 == v2 (struct containing [0]func() cannot be compared)

可以发现,的确有效。因为 func() 是一个函数,而函数在 Go 中是不可比较的。

既然使用 map[string]int_ [0]func() 都能实现禁止结构体相等性比较,那么我为什么说 _ [0]func() 是更优雅的做法呢?

_ [0]func() 有着比其他实现方式更优的特点:

它不占内存空间!

使用匿名字段 _ 语义也更强。

而且,我们直接去 Go 源码里搜索,能够发现其实 Go 本身也在多处使用了这种用法:

1f932d84acb1aa62a664cea42692d9cb.png

_ [0]func()

所以推荐使用 _ [0]func() 来实现禁用结构体相等性比较。

不过值得注意的是:当使用 _ [0]func() 时,不要把它放在结构体最后一个字段,推荐放在第一个字段。这与结构体内存对齐有关,我在《Go 中空结构体惯用法,我帮你总结全了!》 一文中也有提及。

NOTE: 对于 _ [0]func() 不占用内存空间的验证,就交给你自己去实验了。提示:可以使用 fmt.Println(unsafe.Sizeof(v1), unsafe.Sizeof(v2)) 分别打印结构体 Value 的两个实例 v1v2 的内存大小。你可以删掉 _ [0]func() 字段再试一试。

总结

好了,在 Go 中如何让结构体不可比较这个小 Tips 就分享给大家了,还是比较有意思的。

我在看到 slog.Value 源码使用 _ [0]func() 来禁用结构体相等性比较时,又搜索了 Go 的源码中多处在使用,我想这应该是社区推荐的做法了。然后就尝试去网上搜索了下,还真被我搜索到了一个叫 Phuong Le 的人在 X 上发布了 Golang Tip #50: Make Structs Non-comparable. 专门来介绍这个 Tip,并且我在中文社区也找到了鸟窝老师在《Go语言编程技巧》中的译文 Tip #50 使结构体不可比较。

这也印证了我的猜测,_ [0]func() 在 Go 社区中是推荐用法。

本文示例源码我都放在了 GitHub 中,欢迎点击查看。

希望此文能对你有所启发。

延伸阅读

  • slog 源码: https://github.com/golang/go/blob/master/src/log/slog/value.go#L21

  • Golang Tip #50: Make Structs Non-comparable.: https://x.com/func25/status/1768621711929311620

  • Go语言编程技巧: https://colobu.com/gotips/050.html

  • 本文 GitHub 示例代码:https://github.com/jianghushinian/blog-go-example/tree/main/struct/non-comparable

- END -


推荐阅读:

6 个必须尝试的将代码转换为引人注目的图表的工具

Go 1.23新特性前瞻

Gopher的Rust第一课:第一个Rust程序

Go早期是如何在Google内部发展起来的

2024 Gopher Meetup 武汉站活动

go 中更加强大的 traces

「GoCN酷Go推荐」我用go写了魔兽世界登录器?

Go区不大,创造神话,科目三杀进来了

想要了解Go更多内容,欢迎扫描下方👇关注公众号,扫描 [实战群]二维码  ,即可进群和我们交流~


- 扫码即可加入实战群 -

f5901778766749a65487839a69dcbb60.png

35d35f06edbe1891b613178fee1fa191.png

分享、在看与点赞Go d8e2f9a35d485c4ea2f98028f167b0da.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值