Upgrade to Pro — share decks privately, control downloads, hide ads and more …

早すぎ?超先読み Go 1.26 Draft - Preview the contents o...

早すぎ?超先読み Go 1.26 Draft - Preview the contents of the Go 1.26 Draft Release Notes

Avatar for tom twinkle

tom twinkle

November 03, 2025
Tweet

More Decks by tom twinkle

Other Decks in Programming

Transcript

  1. © 2025 ANDPAD All Rights Reserved. 早すぎ?超先読み Go 1.26 Draft

    Preview the contents of the Go 1.26 Draft Release Notes tomtwinkle techlead@andpad
  2. © 2025 ANDPAD All Rights Reserved. ANDPADとは 社内 現場 営業

    / 監督 / 設計 事務 / 管理職 職人 / 業者 メーカー / 流通 現場の効率化から経営改善まで一元管理できる クラウド型建設プロジェクト管理サービス 案件管理 資料 工程表 写真 報告 チャット 黒板 図面 受発注 • • • 
 

  3. © 2025 ANDPAD All Rights Reserved. https://tip.golang.org/doc/go1.26 https://github.com/golang/go/milestone/373 以下気になったものだけ •

    #45624 new(expression) • #73581 Green Tea garbage collector • #51945 errors.AsType[T](err) • #71287 testing ArtifactDir • #68021 go/ast ParseDirective • #71859 go fix modernize Go 1.26 Draft Release Note ※注意※ あくまでDraftの内容なのでこれから変更がある可能性があります。 これから話すのはスライドを作成した 2025/10/24時点での情報です。
  4. © 2025 ANDPAD All Rights Reserved. new(expression) https://go.dev/issue/45624 // ポインタ引数を取る関数呼出し

    (Go < 1.26) timeout := 5 * time.Second client.SetTimeout(&timeout) // ポインタ引数を取る関数呼出し (Go 1.26) client.SetTimeout(new(5 * time.Second)) // 関数からの戻り値へのポインタ (Go < 1.26) val := someFunc() p := &val // 関数からの戻り値へのポインタ (Go 1.26) p := new(someFunc()) &演算子は「メモリ上にある変数のアド レス」を取得するもの プリミティブ型のポインタに直接値を設 定する術がありませんでした。 大体みんなToPointer みたいなボイ ラープレートコードを用意していました が、それが不要になります。
  5. © 2025 ANDPAD All Rights Reserved. new(expression) https://go.dev/issue/45624 この機能の設計過程では、主に 2つの

    選択肢が検討されました。 1つはnewの拡張、もう1つは型変換を アドレス可能にする(例: &int(3))という 案です。 定数のアドレスを取る( &3)という案も 議論されましたが、型付けされていな い定数はメモリ上に具体的な実体を持 たないため、ポインタの指す先が存在 しないという理由で実現不可能と判断 されました。
  6. © 2025 ANDPAD All Rights Reserved. リリパで発表された永見さんのスライドが分かりやすいのでオススメ GC25 Recap+: Advancing

    Go Garbage Collection with Green Tea https://speakerdeck.com/logica0419/gc25-recap-green-tea-gc Green Tea garbage collector https://go.dev/issue/73581 CPUとメモリの速度差により、 GoのGC はキャッシュミスでCPUを浪費していま す。 「Green Tea GC」は、オブジェクト単位 でなく8KiBの「ページ」単位でマーキン グを遅延させ、物理的に近いオブジェ クトを一括処理します。 これによりキャッシュ効率を改善し SIMD高速化を図る施策です。 特にファンアウト数が大きいオブジェク トが多い場合は有効。 ファンアウト数が小さい場合は効果が 薄いので既存のマークスイープアルゴ リズムでのGCを利用します。
  7. © 2025 ANDPAD All Rights Reserved. GOEXPERIMENT=greenteagc つければ Go 1.25でも使えます。Go

    1.26で正式に採用される見込み。 Green Tea GCのThe Go Blogが2日前にリリースされました。分かりやすい。 https://go.dev/blog/greenteagc Green Tea garbage collector https://go.dev/issue/73581
  8. © 2025 ANDPAD All Rights Reserved. errors.AsType[T](err) https://go.dev/issue/51945 // Go

    < 1.26 https://go.dev/play/p/RxC49tUL5eU func main() { err := NewCustomError() var cerr CustomError if errors.As(err, &cerr) { fmt.Printf("%+v", cerr) } } // Go 1.26 https://go.dev/play/p/dIZ6PQduwcf?v=gotip func main() { err := NewCustomError() if cerr, ok := errors.AsType[CustomError](err); ok { fmt.Printf("%+v", cerr) } } errorのinterfaceを持つカスタムエラー を利用した場合、今までは erros.As(err, &Err)を利用してカスタム エラーのマッピングを行う必要がありま した。 Go1.26ではGenericsを利用した errors.AsType[T](err) で変換したいカ スタムエラーを指定出来ます。
  9. © 2025 ANDPAD All Rights Reserved. testing ArtifactDir https://go.dev/issue/71287 //

    Go 1.26 https://go.dev/play/p/CSH38BByd-v?v=gotip func TestFoo(t *testing.T) { fmt.Println(t.ArtifactDir()) } // go test -artifacts 未指定 なら t.TempDir() と同様 // go test -artifacts <dirpath>指定あり なら 指定したパスが返される go test -artifactsフラグを設定しない場 合はt.TempDir()と同様にテンポラリの ディレクトリを返します。 go test -artifactsフラグを設定した場合 はそのディレクトリを返します。
  10. © 2025 ANDPAD All Rights Reserved. go/ast ParseDirective https://go.dev/issue/68021 //

    Go 1.26 https://cs.opensource.google/go/go/+/master:src/go/ast/directive.go;bpv=1;bpt=0 // //namespace:directive args 形式のディレクティブコメント type Directive struct { Tool string // "go" や "mystuff" などのツール名 Name string // "generate"や"embed" などのディレクティブ名 Args string // "stringer -type Op" などの引数文字列 Pos token.Pos// ディレクティブの "//" の開始位置 ArgsPos token.Pos // 引数(Args)の開始位置 } // text は "//" を含む単一の行コメントでなければならない。 // pos はそのコメントの開始位置( token.Pos)。 // ディレクティブとして解析できた場合、 Directive構造体と true を返す。 func ParseDirective(pos token.Pos, text string) (Directive, bool) // ParseArgs によって解析されたディレクティブの引数を表す。 type DirectiveArg struct { Arg string // 解釈された引数(クォートは削除済み) Pos token.Pos // 引数の開始位置 } // スペース区切り、 " " または ` ` で囲まれた文字列に従って解析 func (d Directive) ParseArgs() ([]DirectiveArg, error) Go 1.23以降、ディレクティブの形式 (//namespace:directive args)が標準 化され、サードパーティも利用可能にな りましたが 今まで ast.CommentGroupを ”//mylinter:hoge” のような文字列で判 別するしかありませんでした。 ParseDirectiveでDirective構造体に Parseすることが可能になりディレク ティブコメントがより使いやすくなりまし た。
  11. © 2025 ANDPAD All Rights Reserved. go fix modernize https://go.dev/issue/71859

    https://go.dev/blog/introducing-gofix gofix (go tool fix, go fix) は Go 1 リリースま でドラスティックに変更が入っていたそれ以 前の古いコードを Go 1 の構文に一括変換 するツール。 詳しくはRuss Coxが書いたGo Blog記事を 参照 https://go.dev/blog/introducing-gofix 後方互換性を維持している Go 1 以降は死 に機能だった
  12. © 2025 ANDPAD All Rights Reserved. go fix modernize https://go.dev/issue/71859

    古いcmd/fixツールを廃止 (https://go.dev/issue/73605)し、go fixを go/analysisを利用してgo vetコマンドのラッ パーとして機能するように再構築します 。 もう役目を終えたfixersは削除され、Go 1.18以降徐々に増えてきた新しい構文に対 応したfixersが追加されます。 Go 0.x 未だに使ってる人が Go 1.26に移行 するパターンは困りますが多分ほとんど影 響はないはず。
  13. © 2025 ANDPAD All Rights Reserved. go fix modernize https://go.dev/issue/71859

    // Go < 1.26 goplsのmodernizeを利用する場合 $ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./… // Go 1.26 go fixに統合 $ go fix ./… goplsのmodernizeやgofixをgoの標準 コマンドのfixで利用出来るようにする。 package pkg import pkg2 "pkg/v2" //go:fix inline func F() { pkg2.F(nil) } // Go < 1.26 goplsのfixを利用する場合 $ go run golang.org/x/tools/gopls/internal/analysis/gofix/cmd/gofix@latest -test ./... // Go 1.26 $ go fix ./… ? optionはどうなるか不明そのまま動く?
  14. © 2025 ANDPAD All Rights Reserved. go fix modernize https://go.dev/issue/71859

    goplsのmodernize -fix -testと同等の機能がそのまま実装されそう https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize go fix -diff で 実際にfixせずにdiffのみ表示する機能を提供予定 • forvar: remove x := x variable declarations made unnecessary by the new semantics of loops in go1.22. • slicescontains: replace 'for i, elem := range s { if elem == needle { ...; break }' by a call to slices.Contains, added in go1.21. • minmax: replace an if/else conditional assignment by a call to the built-in min or max functions added in go1.21. • sortslice: replace sort.Slice(s, func(i, j int) bool) { return s[i] < s[j] } by a call to slices.Sort(s), added in go1.21. • efaceany: replace interface{} by the 'any' type added in go1.18. • mapsloop: replace a loop around an m[k]=v map update by a call to one of the Collect, Copy, Clone, or Insert functions from the maps package, added in go1.21. • fmtappendf: replace []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...), added in go1.19.
  15. © 2025 ANDPAD All Rights Reserved. go fix modernize https://go.dev/issue/71859

    • testingcontext: replace uses of context.WithCancel in tests with t.Context, added in go1.24. • omitzero: replace omitempty by omitzero on structs, added in go1.24. • bloop: replace "for i := range b.N" or "for range b.N" in a benchmark with "for b.Loop()", and remove any preceding calls to b.StopTimer, b.StartTimer, and b.ResetTimer. B.Loop intentionally defeats compiler optimizations such as inlining so that the benchmark is not entirely optimized away. Currently, however, it may cause benchmarks to become slower in some cases due to increased allocation; see https://go.dev/issue/73137. • rangeint: replace a 3-clause "for i := 0; i < n; i++" loop by "for i := range n", added in go1.22. • stringsseq: replace Split in "for range strings.Split(...)" by go1.24's more efficient SplitSeq, or Fields with FieldSeq. • stringscutprefix: replace some uses of HasPrefix followed by TrimPrefix with CutPrefix, added to the strings package in go1.20. • waitgroup: replace old complex usages of sync.WaitGroup by less complex WaitGroup.Go method in go1.25.
  16. © 2025 ANDPAD All Rights Reserved. よかったら はてブ お願いします🙏 Go1.25リリース!

    gopls待望のFileSet.AddExistingFilesの追加の経緯とその 後google/wire,go-swagger等で発生したトラブル - unsafe の必要性と危険性 について - https://tech.andpad.co.jp/entry/2025/10/09/100000 Goの†黒魔術†に対する防衛術 ~Defence Against the Go's dark Arts~ https://tech.andpad.co.jp/entry/2025/09/23/100000