以下の内容はhttps://devlights.hatenablog.com/entry/2026/03/17/073000より取得しました。


Goメモ-667 (Go 1.26 で go fix が変わった)(modernizeパッケージ, go:fix inline)

関連記事

GitHub - devlights/blog-summary: ブログ「いろいろ備忘録日記」のまとめ · GitHub

概要

以下、自分用のメモです。忘れないうちにメモメモ。。。

Go 1.26で go fix に大きな変化が加わったのですが、それについてまとめようと思ったら、既に素晴らしい記事があったのでそちらをメモメモ。。。感謝m( )m

blog.utgw.net

また、公式ブログからも以下の記事がアップされてますね。

go.dev

go.dev

お試し

どのようなアナライザーがあるのかは以下で見れます。

$ go tool fix help
・
・
・
Registered analyzers:

    any          replace interface{} with any
    buildtag     check //go:build and // +build directives
    fmtappendf   replace []byte(fmt.Sprintf) with fmt.Appendf
    forvar       remove redundant re-declaration of loop variables
    hostport     check format of addresses passed to net.Dial
    inline       apply fixes based on 'go:fix inline' comment directives
    mapsloop     replace explicit loops over maps with calls to maps package
    minmax       replace if/else statements with calls to min or max
    newexpr      simplify code by using go1.26's new(expr)
    omitzero     suggest replacing omitempty with omitzero for struct fields
    plusbuild    remove obsolete //+build comments
    rangeint     replace 3-clause for loops with for-range over integers
    reflecttypefor replace reflect.TypeOf(x) with TypeFor[T]()
    slicescontains replace loops with slices.Contains or slices.ContainsFunc
    slicessort   replace sort.Slice with slices.Sort for basic types
    stditerators use iterators instead of Len/At-style APIs
    stringsbuilder replace += with strings.Builder
    stringscut   replace strings.Index etc. with strings.Cut
    stringscutprefix replace HasPrefix/TrimPrefix with CutPrefix
    stringsseq   replace ranging over Split/Fields with SplitSeq/FieldsSeq
    testingcontext replace context.WithCancel with t.Context in tests
    waitgroup    replace wg.Add(1)/go/wg.Done() with wg.Go

By default all analyzers are run.
To select specific analyzers, use the -NAME flag for each one,
 or -NAME=false to run all analyzers not explicitly disabled.



//go:fix inline も何気に素晴らしいですね。

package main

import "fmt"

//go:fix inline
func add(x, y int) int {
        return x + y
}

func main() {
        var (
                x = 10
                y = 12
        )
        fmt.Println(add(x, y))
}

上のソースに

$ go fix .

すると

package main

import "fmt"

//go:fix inline
func add(x, y int) int {
        return x + y
}

func main() {
        var (
                x = 10
                y = 12
        )
        fmt.Println(x + y)
}

ちゃんとインライン化されてます。

参考情報

個人的Goのおすすめ書籍

個人的に読んでとても勉強になった書籍さんたちです。


過去の記事については、以下のページからご参照下さい。

サンプルコードは、以下の場所で公開しています。




以上の内容はhttps://devlights.hatenablog.com/entry/2026/03/17/073000より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14