开发者生态
morning
去1.27
摘要
Today the Go team is pleased to release Go 1.27. You can find its binary archives and installers on the download page . Go 1.27 brings major enhancements across the language, toolchain, runtime, and s...
type
the
and
Rand
func
now
struct
for
int
Burrow
2026-08-20
1 阅读
约4分钟阅读
database64128
字号:
今天,Go 团队很高兴发布 Go 1.27。您可以在下载页面上找到其二进制存档和安装程序。 Go 1.27 在语言、工具链、运行时和标准库方面带来了重大增强。以下是一些主要亮点。语言变化 Go 1.27 引入了对语言规范的三个显着更新。首先,现在支持通用方法。例如,请参阅 math/rand/v2.Rand : // 在 Go 1.27 之前,必须为每种类型添加一个单独的 Rand 方法 // (为简洁起见,省略了无符号整数方法)。 func (r *Rand) Int32N(n int32) int32 func (r *Rand) Int64N(n int64) int64 func (r *Rand) IntN(n int) int // Go 1.27 添加了一个适用于所有整数类型的新泛型方法。 func (r *Rand) N[Int intType](n Int) Int 其次,结构体文字中的键现在可以是该结构体类型的任何有效字段选择器,允许直接初始化嵌套或嵌入结构体中的字段: type Habitat struct { Burrow string } type Gopher struct { Name string Habitat // 嵌入结构体。 } // Go 1.27 允许直接使用 Burrow 作为 key。 g := Gopher{ Name: "Gopher", Burrow: "Burrow #42", } 最后,函数类型推断已被推广到适用于所有赋值上下文。现在,在复合文字、类型转换和通道发送中,无需显式类型参数即可使用泛型函数: func GenericFormatter[T any](v T) string { return fmt.Sprintf("value: %v", v) } type IntFormatter func(int) string // Go 1.27 在复合文字、转换和通道发送中推断 T = int。 formatters := []IntFormatter{GenericFormatter} fn := IntFormatter(GenericFormatter) ch := make(chan IntFormatter, 1) ch <- GenericFormatter go fix 包括几个新的现代化工具:atomictypes、embedlit、slicesbackward 和 unsafefuncs。 go doc 现在支持 package@version 查询,例如 go doc example.com/pkg@v1.2.3 。 go mod tidy 现在自动将 go.mod 中的多个 require 块合并为标准的直接和间接两块结构。性能和运行时 标准库添加 请阅读 Go 1.27 发行说明以获取完整的更改列表和详细信息。在接下来的几周内,后续博客文章将更详细地介绍与 Go 1.27 相关的一些主题。稍后回来阅读这些帖子。感谢通过编写代码、提交错误、尝试实验性添加和测试候选版本对此版本做出贡献的所有人。与往常一样,如果您发现任何问题,请提出问题。我们希望您喜欢使用 Go 1.27!
这篇文章对您有帮助吗?
订阅66必读
每日精选科技资讯,直达你的邮箱