Skip to content

Detects imports, types & functions that can be replaced by the standard library and suggests fixes.

License

Notifications You must be signed in to change notification settings

abemedia/stdlib

Repository files navigation

stdlib

Go Reference CI Go Report Card

A static analysis tool for Go that detects usage of functions from third-party libraries and suggests replacements using standard library functions or built-in language features. It leverages Go’s analysis package to provide automated refactorings and improvement suggestions.

Usage

# Install
go install github.com/abemedia/stdlib/cmd/stdlib@latest

# Use
stdlib ./...

Replacements

See below for all the replacements of packages, functions and types. They will only be replaced if the Go version of the file supports the new package.

Packages

Replaces the imports of packages from golang.org/x which now exist in the stdlib.

Before After
golang.org/x/exp/maps maps
golang.org/x/exp/rand math/rand/v2
golang.org/x/exp/slices slices
golang.org/x/exp/slog log/slog
golang.org/x/net/context context
golang.org/x/sync/syncmap sync

Functions

Expand the sections below to see the supported replacements for each package.

github.com/samber/lo

Chunk

Before:

result := lo.Chunk(slice, size)

After:

result := slices.Chunk(slice, size)

Drop

Before:

a := []int{0, 1, 2, 3, 4, 5}
b := lo.Drop(a, 2)

After:

a := []int{0, 1, 2, 3, 4, 5}
b := a[2:]

DropRight

Before:

a := []int{0, 1, 2, 3, 4, 5}
b := lo.DropRight(a, 2)

After:

a := []int{0, 1, 2, 3, 4, 5}
b := a[:len(a)-2]

Contains

Before:

if lo.Contains(slice, target) {
    // do something
}

After:

if slices.Contains(slice, target) {
    // do something
}

ContainsBy

Before:

if lo.ContainsBy(slice, func(item int) bool {
    return item > 10
}) {
    // do something
}

After:

if slices.ContainsFunc(slice, func(item int) bool {
    return item > 10
}) {
    // do something
}

IndexOf

Before:

idx := lo.IndexOf(slice, target)

After:

idx := slices.Index(slice, target)

Min

Before:

min := lo.Min(slice)

After:

min := slices.Min(slice)

MinBy

Before:

min := lo.MinBy(slice, func(a, b int) bool {
    return a < b
})

After:

min := slices.MinFunc(slice, func(a, b int) int {
    return cmp.Compare(a, b)
})

Max

Before:

max := lo.Max(slice)

After:

max := slices.Max(slice)

MaxBy

Before:

max := lo.MaxBy(slice, func(a, b int) bool {
    return a > b
})

After:

max := slices.MaxFunc(slice, func(a, b int) int {
    return cmp.Compare(a, b)
})

IsSorted

Before:

if lo.IsSorted(slice) {
    // do something
}

After:

if slices.IsSorted(slice) {
    // do something
}

IsSortedByKey

Before:

sorted := lo.IsSortedByKey(slice, func(a string) string {
    return a
})

After:

sorted := slices.IsSortedFunc(slice, func(a, next string) int {
    return cmp.Compare(a, next)
})

Flatten

Before:

flattened := lo.Flatten(sliceOfSlices)

After:

flattened := slices.Concat(sliceOfSlices...)

Keys

Before:

keys := lo.Keys(m)

After:

keys := maps.Keys(m)

Values

Before:

values := lo.Values(m)

After:

values := maps.Values(m)

CoalesceOrEmpty

Before:

result := lo.CoalesceOrEmpty(s1, s2, s3)

After:

result := cmp.Or(s1, s2, s3)

RuneLength

Before:

n := lo.RuneLength(s)

After:

n := utf8.RuneCountInString(s)
github.com/samber/lo/mutable

Reverse

Before:

lo.Reverse(slice)

After:

slices.Reverse(slice)

About

Detects imports, types & functions that can be replaced by the standard library and suggests fixes.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Languages