Go 1.27 正式发布:泛型方法、结构体初始化与工具链现代化

Go 博客

Go 1.27发布

尼古拉斯·胡辛,代表围棋队
2026年8月19日

今天,Go 团队很高兴发布 Go 1.27。你可以在下载页面.

Go 1.27 在语言、工具链、运行时和标准库方面带来了重大改进。以下是一些关键亮点。

语言变化

Go 1.27 引入了三个显著的更新语言规范.

首先,通用方法现在被支持。例如,参见math/rand/v2.Rand:

// Prior to Go 1.27, a separate method on Rand had to be added for each type
// (unsigned integer methods omitted for brevity).
func (r *Rand) Int32N(n int32) int32
func (r *Rand) Int64N(n int64) int64
func (r *Rand) IntN(n int) int

// Go 1.27 adds a new generic method that works for all integer types.
func (r *Rand) N[Int intType](n Int) Int

第二,钥匙在结构字面现在可能有效场选择器对于结构类型,允许嵌套或嵌入结构体中的字段直接初始化:

type Habitat struct {
    Burrow string
}

type Gopher struct {
    Name    string
    Habitat // Embedded struct.
}

// Go 1.27 allows using Burrow as a key directly.
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 infers T = int in composite literals, conversions, and channel sends.
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现在会自动合并多个require区块go.mod形成标准的直接和间接两块结构。

性能与运行时

标准库的补充

请阅读Go 1.27 发布说明完整变更和细节列表。

接下来几周,后续博客文章将更详细地涵盖与Go 1.27相关的话题。稍后请回来阅读这些文章。

感谢所有为本次发布做出贡献的人,包括编写代码、纠正漏洞、尝试实验性添加以及测试候选版本。一如既往,如果你发现任何问题,请务必告知。

提交争议.

希望你喜欢Go 1.27的使用!

上一篇文章:介绍 pkg.go.dev API
博客索引

添加评论
点赞收藏
点踩分享查看原文
评论
?
参与讨论