Go 1.23 新特性深度解析:迭代器、Rangefunc 与性能改进
Go 1.23 Deep Dive: Iterators, Rangefunc and Performance Improvements
| He Tao | 2026-07-10T10:00:00
全面解析 Go 1.23 的重要新特性,包括标准迭代器模式、range over function 语法和运行时性能优化。
Comprehensive analysis of Go 1.23 major new features including standard iterator pattern, range over function syntax, and runtime performance improvements.
迭代器标准化Go 1.23 最重要的特性是将迭代器模式标准化。新增 iter 包定义了两种迭代器签名:package iter // 单值迭代器 type Seq[V any] func(yield func(V) bool) // 键值对迭代器 type Seq2[K, V any] func(yield func(K, V) bool)Range over Function配合迭代器,for-range 语法现在支持遍历函数:// 自定义可遍历类型 func (t *Tree[V]) All() iter.Seq2[string, V] { return func(yield func(string, V) bool) { t.walk(func(k string, v V) bool { return yield(k, v) }) } } // 使用 for key, value := range tree.All() { fmt.Println(key, value) }slices 和 maps 包增强slices.Collect:从迭代器收集为切片slices.Sorted:排序迭代器maps.Keys / maps.Values:返回迭代器性能改进runtime:GC 暂停时间减少 40%crypto/tls:TLS 1.3 握手性能提升 15%encoding/json:大 JSON 解析速度提升 20%net/http:HTTP/2 连接复用优化
Iterator StandardizationThe most important feature in Go 1.23 is the standardization of the iterator pattern. The new iter package defines two iterator signatures:package iter // Single-value iterator type Seq[V any] func(yield func(V) bool) // Key-value iterator type Seq2[K, V any] func(yield func(K, V) bool)Range over FunctionPaired with iterators, for-range syntax now supports ranging over functions:// Custom iterable type func (t *Tree[V]) All() iter.Seq2[string, V] { return func(yield func(string, V) bool) { t.walk(func(k string, v V) bool { return yield(k, v) }) } } // Usage for key, value := range tree.All() { fmt.Println(key, value) }slices and maps Package Enhancementsslices.Collect: collect from iterator into sliceslices.Sorted: sorted iteratormaps.Keys / maps.Values: return iteratorsPerformance Improvementsruntime: GC pause time reduced by 40%crypto/tls: TLS 1.3 handshake performance improved 15%encoding/json: large JSON parsing speed improved 20%net/http: HTTP/2 connection multiplexing optimization