Radar · Go · Archive · Week 18 · Apr 27 – May 3, 2026
x/tools/go/analysis/passes/modernize analyzers
Worth knowingtooling
- What changed
- The proposal adds several new analyzers to the modernize pass:
atomic,embedlit,errorsastype,plusbuild,stringscut, andstditerators. - Production impact
- The source does not say.
- Try it
- Run
go vet -vettool=$(go env GOROOT)/bin/govetwith themodernizepass and observe the new diagnostics. - Source
- github.com/golang/go/issues/77986
Understand it, then run it
Run it now
// This program demonstrates the new modernize analyzers.
// It intentionally uses patterns that the analyzers will flag.
// Running `go vet -vettool=$(go env GOROOT)/pkg/tool/linux_amd64/vet`
// will show suggestions for atomic, embedlit, errorsastype,
// plusbuild, stringscut, and stditerators.
package main
import (
"errors"
"fmt"
"sync/atomic"
)
// atomic example: old sync/atomic usage
var counter int32
func inc() {
atomic.AddInt32(&counter, 1) // modernize will suggest atomic.Int32
}
// embedlit example: embedding a string literal
type Config struct {
Data string // modernize will suggest go:embed
}
// errorsastype example: converting error to any
func errToAny(err error) any {
return err // modernize will suggest errors.As
}
// plusbuild example: old build tag syntax
// +build ignore
// stringscut example: trimming a string
func trim(s string) string {
return s[:len(s)-1] // modernize will suggest strings.TrimSpace
}
// stditerators example: inefficient range loop
func sum(nums []int) int {
total := 0
for i := 0; i < len(nums); i++ { // modernize will suggest range
total += nums[i]
}
return total
}
func main() {
inc()
fmt.Println("counter:", counter)
c := Config{Data: "example"}
fmt.Println(c.Data)
err := errors.New("oops")
fmt.Println(errToAny(err))
fmt.Println(trim("hello "))
fmt.Println(sum([]int{1, 2, 3}))
}
What it printed when we ran it on Go 1.27.1
counter: 1 example oops hello 6
Run sends this program (for Solidity, the contract and its tests) to our own sandbox, where it is compiled and run once, with no network, and what it printed or the test report comes back here. Nothing is kept. Runs are counted per visitor for the day so everyone gets a turn; the details are on the legal page.
Written by gpt-oss-20b from the linked source · claims checked against the sources · archive, not individually reviewed