Loading...
Loading...
Apply modern Go syntax guidelines based on project's Go version. Use when user ask for modern Go code guidelines.
npx skill4agent add jetbrains/go-modern-guidelines use-modern-gogrep -rh "^go " --include="go.mod" . 2>/dev/null | cut -d' ' -f2 | sort | uniq -c | sort -nr | head -1 | xargs | cut -d' ' -f2 | grep . || echo unknownslicesmapscmptime.Sincetime.Since(start)time.Now().Sub(start)time.Untiltime.Until(deadline)deadline.Sub(time.Now())errors.Iserrors.Is(err, target)err == targetanyanyinterface{}bytes.Cutbefore, after, found := bytes.Cut(b, sep)strings.Cutbefore, after, found := strings.Cut(s, sep)fmt.Appendfbuf = fmt.Appendf(buf, "x=%d", x)[]byte(fmt.Sprintf(...))atomic.Boolatomic.Int64atomic.Pointer[T]atomic.StoreInt32var flag atomic.Bool
flag.Store(true)
if flag.Load() { ... }
var ptr atomic.Pointer[Config]
ptr.Store(cfg)strings.Clonestrings.Clone(s)bytes.Clonebytes.Clone(b)strings.CutPrefix/CutSuffixif rest, ok := strings.CutPrefix(s, "pre:"); ok { ... }errors.Joinerrors.Join(err1, err2)context.WithCancelCausectx, cancel := context.WithCancelCause(parent)cancel(err)context.Causecontext.Cause(ctx)minmaxmax(a, b)clearclear(m)clear(s)slices.Containsslices.Contains(items, x)slices.Indexslices.Index(items, x)slices.IndexFuncslices.IndexFunc(items, func(item T) bool { return item.ID == id })slices.SortFuncslices.SortFunc(items, func(a, b T) int { return cmp.Compare(a.X, b.X) })slices.Sortslices.Sort(items)slices.Maxslices.Minslices.Max(items)slices.Reverseslices.Reverse(items)slices.Compactslices.Compact(items)slices.Clipslices.Clip(s)slices.Cloneslices.Clone(s)maps.Clonemaps.Clone(m)maps.Copymaps.Copy(dst, src)maps.DeleteFuncmaps.DeleteFunc(m, func(k K, v V) bool { return condition })sync.OnceFuncf := sync.OnceFunc(func() { ... })sync.Oncesync.OnceValuegetter := sync.OnceValue(func() T { return computeValue() })context.AfterFuncstop := context.AfterFunc(ctx, cleanup)context.WithTimeoutCausectx, cancel := context.WithTimeoutCause(parent, d, err)context.WithDeadlineCausefor i := range nfor i := range len(items)for i := 0; i < len(items); i++cmp.Orcmp.Or(flag, env, config, "default")// Instead of:
name := os.Getenv("NAME")
if name == "" {
name = "default"
}
// Use:
name := cmp.Or(os.Getenv("NAME"), "default")reflect.TypeForreflect.TypeFor[T]()reflect.TypeOf((*T)(nil)).Elem()http.ServeMuxmux.HandleFunc("GET /api/{id}", handler)r.PathValue("id")maps.Keys(m)maps.Values(m)slices.Collect(iter)slices.Sorted(iter)keys := slices.Collect(maps.Keys(m)) // not: for k := range m { keys = append(keys, k) }
sortedKeys := slices.Sorted(maps.Keys(m)) // collect + sort
for k := range maps.Keys(m) { process(k) } // iterate directlyt.Context()context.WithCancel(context.Background())func TestFoo(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
result := doSomething(ctx)
}func TestFoo(t *testing.T) {
ctx := t.Context()
result := doSomething(ctx)
}omitzeroomitemptytype Config struct {
Timeout time.Duration `json:"timeout,omitempty"` // doesn't work for Duration!
}type Config struct {
Timeout time.Duration `json:"timeout,omitzero"`
}b.Loop()for i := 0; i < b.N; i++func BenchmarkFoo(b *testing.B) {
for i := 0; i < b.N; i++ {
doWork()
}
}func BenchmarkFoo(b *testing.B) {
for b.Loop() {
doWork()
}
}strings.SplitSeqstrings.Splitfor _, part := range strings.Split(s, ",") {
process(part)
}for part := range strings.SplitSeq(s, ",") {
process(part)
}strings.FieldsSeqbytes.SplitSeqbytes.FieldsSeqwg.Go(fn)wg.Add(1)go func() { defer wg.Done(); ... }()var wg sync.WaitGroup
for _, item := range items {
wg.Add(1)
go func() {
defer wg.Done()
process(item)
}()
}
wg.Wait()var wg sync.WaitGroup
for _, item := range items {
wg.Go(func() {
process(item)
})
}
wg.Wait()new(val)x := val; &xx := val; &xtimeout := 30
debug := true
cfg := Config{
Timeout: &timeout,
Debug: &debug,
}cfg := Config{
Timeout: new(30), // *int
Debug: new(true), // *bool
}errors.AsType[T](err)errors.As(err, &target)var pathErr *os.PathError
if errors.As(err, &pathErr) {
handle(pathErr)
}if pathErr, ok := errors.AsType[*os.PathError](err); ok {
handle(pathErr)
}