-
-
Save up1/bf52784b10f7478d300f6c1e5d368982 to your computer and use it in GitHub Desktop.
Go fix 1.26
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Until Go 1.25 | |
| age := 42 | |
| p := &age | |
| // Go 1.26 onwards | |
| p := new(42) | |
| // run go fix | |
| $go fix -newexpr ./... | |
| // Simple workflow | |
| go fix ./... | |
| go test ./... | |
| git diff |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $go version | |
| go version go1.26.0 darwin/arm64 | |
| $go tool fix help | |
| fix is a tool for static analysis of Go programs. | |
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ดูการเปลี่ยนแปลงก่อน | |
| $go fix -diff ./... | |
| // ทำการเปลี่ยนแปลง code | |
| $go fix ./... | |
| $GOOS=darwin GOARCH=arm64 go fix ./... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment