3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Go1.26 リリースノート 気になる所をピックアップ

3
Last updated at Posted at 2025-12-02

本記事でやること

Go1.26のリリースノートを読んで個人的に気になる点をピックアップしてまとめます。

Changes to the language

組み込み関数のnew関数が式を入力として受け取れるようになり、変数の初期値を指定できるようになります。

何が便利になるのか

G1.25まではポインタに値を定義するには一度変数を作る必要がありました。

Go1.25まで
type Person struct {
	Name string
	Age  *int
}

func MyFunc() int {
	return 2
}

func main() {
	val := MyFunc()
	person := Person{
		Name: "taro",
		Age:  &val,
	}
}

new関数は式を受け取れるようになるので、1行で書くことが可能になります。一時変数を作らずに、直接ポインタにできるのでコードが短く、読みやすくなります。

Go1.26から
type Person struct {
	Name string
	Age  *int
}

func MyFunc() int {
	return 2
}

func main() {
	person := Person{
		Name: "taro",
		Age:  new(MyFunc()),
	}
	fmt.Println(person)
}

Tools

Go command

cmd/docgo tool docの削除

cmd/docgo tool docが削除され、go docに機能が統一されます。同じフラグと引数を受け取り、同じ動作をします。

go fixのリプレイス

go vet と同じアナライザをgo fixで使用できるようになります。go fixコマンドで従来使われてきた修正ツールが削除され、新しい言語機能やライブラリを使うための修正を提案するアナライザに置き換えられます。

何が便利になるのか

こちらのProposalのBackgroundに記述があるように、goplsに含まれるmodernize//go:fix inlineなどGoの最新バージョンの機能を使うように修正しくれるアナライザーをgo fixからも使えるようなるということです。

Background: gopls includes a number of analyzers in the Go analysis framework whose purpose is to modernize Go code to take advantage of newer features of the language and library. For example, the modernize suite, and the //go:fix inline analyzer. We would like to make these available through go fix, in much the same way that analyzers for reporting diagnostics are available through go vet.

今までmodernizeパッケージを実行するためには、以下のような長いコマンドを実行する必要がありました。これがgo fixコマンドを通して使えるようになります。

$ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...

Bootstrap

Go 1.26をビルドするには、Go 1.24.6以降が必要になります。Go 1.28では、Go 1.26のマイナーリリース以降が必要になる予定です。

Standard library

Minor changes to the library

errors

errors.AsType関数が追加されます。

何が便利になるのか

エラー型の判定で用いられ、同様の機能としてerrors.Asがあるのですが、以下のように都度エラーのポインタ変数を定義する必要がなくなります。

Go1.25まで
type myError struct {
	msg string
}

func (e *myError) Error() string {
	return e.msg
}

func NewMyError() error {
	return &myError{"this is my error"}
}

func main() {
	err := NewMyError()
	var myErr *myError
	if errors.As(err, &myErr) {
		fmt.Println("~Go1.25:", myErr)
	}
}
Go1.26から
type myError struct {
	msg string
}

func (e *myError) Error() string {
	return e.msg
}

func NewMyError() error {
	return &myError{"this is my error"}
}

func main() {
	err := NewMyError()
	if myErr, ok := errors.AsType[*myError](err); ok {
		fmt.Println("Go1.26~:", myErr)
	}
}
3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?