> ## Content Index
> Fetch the complete content index at: https://huizhou92.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Golang 1.23: 新的 unique 包
- URL: https://huizhou92.com/golang-1-23-xin-de-unique-bao/
- Published: 2024-06-04T01:54:42.000Z
- Updated: 2026-09-06T11:39:32.000Z
- Author: huizhou92
- Tags: tech, golang, #lang-zh-cn

上周，[Go 1.23](https://tip.golang.org/doc/go1.23?ref=huizhou92.com) 进入冻结期，这意味着不会添加任何新功能，并且任何已添加的功能不太可能被删除。这是一个预览即将发生的变化的好机会。  
这篇文章，我们来介绍引入的新包unique

> This article is first published in the medium MPP plan. If you are a medium user, please follow me in [medium](https://medium.huizhou92.com/?ref=huizhou92.com). Thank you very much.

根据[wikipedia](https://en.wikipedia.org/wiki/Interning%5F%28computer%5Fscience%29?ref=huizhou92.com)的描述，interning是按需重复使用具有同等值对象的技术，减少创建新对象的动作。这种创建模式经常用于不同编程语言中的数和字符串，可以避免不必要的对象重复分配的开销。  
unique 参考了[go4.org/intern](https://github.com/go4org/intern?ref=huizhou92.com) ,将它移动到了 官方库，并且做了相应的修改。 [issue #62483](https://github.com/golang/go/issues/62483?ref=huizhou92.com)

就像官方描述的一样 unique 这个包提供了一种轻量化（unique仅仅八个字节）的比较两个变量是否相等的实现。比如下面这段代码

[View code snippet on GitHub Gist →](https://gist.github.com/hxzhouh/552a3cf5718836c54639b828b868bd02?ref=huizhou92.com)

性能提升还是很明显的

```shell
➜  unique git:(master) ✗ /Users/hxzhouh/workspace/googlesource/go/bin/go test -bench='BenchmarkMake1' -count=5 
goos: darwin
goarch: arm64
pkg: unique
cpu: Apple M1 Pro
BenchmarkMake1-10       122033748                9.692 ns/op
BenchmarkMake1-10       123878858                9.688 ns/op
BenchmarkMake1-10       123927121                9.706 ns/op
BenchmarkMake1-10       123849468                9.759 ns/op
BenchmarkMake1-10       123306187                9.673 ns/op
PASS
ok      unique  11.055s
➜  unique git:(master) ✗ /Users/hxzhouh/workspace/googlesource/go/bin/go test -bench='BenchmarkMake2' -count=5
goos: darwin
goarch: arm64
pkg: unique
cpu: Apple M1 Pro
BenchmarkMake2-10       1000000000               0.3118 ns/op
BenchmarkMake2-10       1000000000               0.3114 ns/op
BenchmarkMake2-10       1000000000               0.3119 ns/op
BenchmarkMake2-10       1000000000               0.3136 ns/op
BenchmarkMake2-10       1000000000               0.3115 ns/op
PASS
ok      unique  1.875s

```

但是 你不应该把他当作一个全局变量来用,存储共享数据，unique 的底层实现其实是一个map,查询的成本也是很高的。  
比如  
[View code snippet on GitHub Gist →](https://gist.github.com/hxzhouh/e8102d4c840e48501a8b84baddb7fe0b?ref=huizhou92.com)

```shell
➜  huizhou92_test git:(master) ✗ /Users/hxzhouh/workspace/googlesource/go/bin/go test --bench=BenchmarkBusinessUnique --count=5 
goos: darwin
goarch: arm64
pkg: huizhou92_test
cpu: Apple M1 Pro
BenchmarkBusinessUnique-10          3114            373867 ns/op
BenchmarkBusinessUnique-10          3280            390818 ns/op
BenchmarkBusinessUnique-10          2941            376503 ns/op
BenchmarkBusinessUnique-10          3291            389665 ns/op
BenchmarkBusinessUnique-10          2954            398610 ns/op
PASS
ok      huizhou92_test  6.320s
➜  huizhou92_test git:(master) ✗ /Users/hxzhouh/workspace/googlesource/go/bin/go test --bench=BenchmarkBusinessString --count=5 
goos: darwin
goarch: arm64
pkg: huizhou92_test
cpu: Apple M1 Pro
BenchmarkBusinessString-10      526721706                2.185 ns/op
BenchmarkBusinessString-10      548612287                2.183 ns/op
BenchmarkBusinessString-10      549425077                2.188 ns/op
BenchmarkBusinessString-10      549012100                2.182 ns/op
BenchmarkBusinessString-10      548929644                2.183 ns/op
PASS
ok      huizhou92_test  7.237s

```

正是因为这样，关于unique的讨论其实还在继续，可能是因为用到的地方不是很多？不管怎么样， 这个新的包进入标准库已经是事实了。`net/netip` 已经用`unique` 重构了它，用来比对IP地址的详细信息。