Open6

Go release notes 追っかけ

momotaro98momotaro98

Go 1.20

Go 1.20 release note

Ref

https://zenn.dev/koya_iwamura/articles/bb9b590b57d825

Cgo cgoを利用しないことがデフォルトになった。

CGO_ENABLEDはデフォルトでOff(0)になり、CGO_ENABLED=0 go build ...のように記載しなくてよくなった。

また、標準ライブラリで現状でcgoを使っているのは、net, os/user, plugin パッケージの3つであるが、MacOSに関してはnet, os/user パッケージにてcgoを使わないように修正されたとのこと。

The packages in the standard library that use cgo are net, os/user, and plugin. On macOS, the net and os/user packages have been rewritten not to use cgo: the same code is now used for cgo and non-cgo builds as well as cross-compiled builds.
A consequence is that, on macOS, if Go code that uses the net package is built with -buildmode=c-archive, linking the resulting archive into a C program requires passing -lresolv when linking the C code.

→ WANT: 本当にそうなのか、cgoあるなしをLinuxとMacで比較して、記事にしたい。

Cover アプリケーションコード、Integration TestとしてCoverageのプロファイルができるようになった。

https://go.dev/testing/coverage/

以下のように使うらしい。(go tool covdataは1.20からの新しいサブコマンド)

# ↓coverageファイルの生成取得
$ go build -cover -o myprogram.exe myprogram.go
# ↓coverageファイルの分析
$ go tool covdata <mode> -i=<dir1,dir2,...> ...flags...

Wrapping multiple errors Error Wrapを複数エラー同時にできるようになった。

err1 := errors.New("err1")
err2 := errors.New("err2")

// 複数Error Wrapのやり方1: 複数の %w を用いる場合
err3 := fmt.Errorf("err: %w, %w", err1, err2)

// 複数Error Wrapのやり方2: errors.Join を用いる場合
err4 := errors.Join(err1, err2)

Go Playground

HTTP ResponseController 各HTTPハンドラごとにタイムアウト等を設定できるResponseControllerが登場

func RequestHandler(w ResponseWriter, r *Request) {
  rc := http.NewResponseController(w)
  rc.SetWriteDeadline(time.Time{}) // disable Server.WriteTimeout when sending a large response
  io.Copy(w, bigData)
}

既存のResponseWriterSetWriteDeadlineメソッドを追加すると破壊的変更になるため、新しいResponseControllerという構造体型を作成した、とのこと。(ref: https://github.com/golang/go/issues/54136)

momotaro98momotaro98

Go 1.21

Go 1.21 release note

Ref

https://zenn.dev/koya_iwamura/articles/0f24b53dcc179f

Changes to the language min, max ビルトイン関数の登場

最小値、最大値を出せる。( official: https://go.dev/ref/spec#Min_and_max )

Changes to the language clear ビルトイン関数の登場

mapとsliceの要素を削除する。( official: https://go.dev/ref/spec#Clear )

Changes to the language いくつかの型推論の向上

(理解できていない 2023年8月13日時点)

official part

New log/slog package slog (Structured Log) の登場

課題: ログの構造化が公式サポートがなかった。
https://zenn.dev/88888888_kota/articles/7e97ff874083cf

Compiler ランタイム時のProfile情報をコンパイル最適化に用いる技術である、Profile-guide optimization (PGO) がGo1.20ではプレビューであったのがGeneralになった。

momotaro98momotaro98

Go 1.23

https://go.dev/doc/go1.23

## Changes to the language イテレーターの登場

range のloopが以下のイテレーター関数を受け付けるようになった。

func(func() bool)
func(func(K) bool)
func(func(K, V) bool)

詳細はiterパッケージを参照。

イテレーターに関するモチベーションの議論はこちらのIssue参照

### iterator解説記事 Go 1.23で導入されたiteratorは何を解決し、なぜ今の形になったのか

## Standard library Timer changes

time.Timer と time.Tickerの仕様が変わったようだがよく理解できていない(2024年8月26日時点)

Go 1.23 makes two significant changes to the implementation of time.Timer and time.Ticker.

First, Timers and Tickers that are no longer referred to by the program become eligible for garbage collection immediately, even if their Stop methods have not been called. Earlier versions of Go did not collect unstopped Timers until after they had fired and never collected unstopped Tickers.

Second, the timer channel associated with a Timer or Ticker is now unbuffered, with capacity 0. The main effect of this change is that Go now guarantees that for any call to a Reset or Stop method, no stale values prepared before that call will be sent or received after the call. Earlier versions of Go used channels with a one-element buffer, making it difficult to use Reset and Stop correctly. A visible effect of this change is that len and cap of timer channels now returns 0 instead of 1, which may affect programs that poll the length to decide whether a receive on the timer channel will succeed. Such code should use a non-blocking receive instead.

## Standard library New unique package

新しいunique package は ( “interning” または “hash-consing”のような) 値を正規化する機能を提供する。

The unique package provides facilities for canonicalizing ("interning") comparable values.

以下のように記載があるが比較可能な(comparable)の意味がわかっていない(2024年8月26日時点)
→ Heapメモリ内で圧縮するにあたってその判断のために型がcomparableである必要があるということ (2024年9月4日時点)

Any value of comparable type may be canonicalized with the new Make[T] function, which produces a reference to a canonical copy of the value in the form of a Handle[T]. Two Handle[T] are equal if and only if the values used to produce the handles are equal, allowing programs to deduplicate values and reduce their memory footprint. Comparing two Handle[T] values is efficient, reducing down to a simple pointer comparison.

→ 以下の解説スライドでの内容を画像で拝借

### The Go Blog - New unique package ← 公式Blogでの解説

## Standard library New structs package

## 標準パッケージ更新 わかりやすい解説スライド 2024/09/04

https://speakerdeck.com/logica0419/go123-std-lib

このスライドのおかげで概要が理解できた。ありがたい。

momotaro98momotaro98

Go 1.24

Go 1.24 release note

Changes to the language 型エイリアスがgeneric型(型パラメーター)にも使えるようになった (言語仕様変更)

Go 1.24でジェネリックな型エイリアスが完全にサポートされました。これにより、

type A[T any] = B[T]

のように、型エイリアス自体を型パラメータでジェネリクス化できます。この機能はコードの柔軟性と再利用性を高め、Go 1.25で標準となる予定です。

Standard library os.DirFS の新しいos.Root

特定のディレクトリツリー内にファイルシステム操作を制限するos.Root型が追加されました。これにより、指定されたディレクトリ外へのアクセスを防ぎ、セキュリティを向上させることができます。

Standard library testing.B.Loop によるベンチマークの改善

testing.B.Loopメソッドが導入され、ベンチマークのイテレーションをより高速かつエラーが発生しにくい方法で実行できるようになりました。これにより、より正確なベンチマーク結果を得やすくなります。

Standard library runtime.AddCleanup によるファイナライザの改善

runtime.SetFinalizerよりも柔軟で効率的なファイナライズメカニズムとして、runtime.AddCleanup関数が追加されました。これにより、SetFinalizerの一部の課題は解決され、リソースのクリーンアップ処理をより確実に行うことができます。
1.24以降は SetFinalizer ではなく AddCleanup に置き換えるべきである。

My note: そもそも runtime.SetFinalizer はインスタンスがGoランタイムのGCに拾われる時に任意の処理をさせるユーザーへのインターフェース。

Standard library 新しいweakパッケージ

弱いポインタをサポートするweakパッケージが新しく追加されました。
Goのweakパッケージは、メモリの自動解放(ガベージコレクション)の邪魔をしない「弱い参照」という特殊な参照方法を提供します。通常の変数(強い参照)がデータを指している間、そのデータはメモリから削除されませんが、弱い参照はデータを参照していても、そのデータの寿命を延ばすことはありません。そのため、他に強い参照がなくなると、弱い参照が残っていてもデータは自動的にメモリから回収されます。この特性は、メモリ効率の良いキャッシュの実装や、循環参照によるメモリリークの回避といった、高度なメモリ管理が必要な場面で役立ちます。

Standard library FIPS 140-3 準拠の促進

FIPS 140-3 準拠を促進するための新しいメカニズムが導入されました。これにより、政府機関や規制の厳しい業界で求められるセキュリティ要件を満たすアプリケーションを開発しやすくなります。

Detail is here, FIPS 140-3 Compliance

momotaro98momotaro98

Go 1.25

https://go.dev/doc/go1.25

Changes to the language

このバージョンでは言語仕様の追加修正は無し

Standard library testing/synctest パッケージの追加

並行処理コードをテストするためのパッケージです。テストを仮想時間内で実行し、全てのゴルーチンがブロックされると瞬時に時間を進めることができます。これにより、時間経過に依存する複雑な並行処理のテストを、決定論的かつ効率的に行うことが可能になります。

Liberay doc page: https://pkg.go.dev/testing/synctest

検証記事: https://zenn.dev/canary_techblog/articles/ec8a96b4541685

Standard library 【experimental】 encoding/json/v2 パッケージの実験的実装

GOEXPERIMENT=jsonv2で有効化できる、新しい実験的なJSON実装です。パフォーマンスと正確性の向上を目指しており、既存のencoding/jsonパッケージを置き換える形で動作します。APIはまだ変更の可能性があり、互換性は保証されていません。