A fast Go slices codec
Install with:
go get github.com/hamba/slicesContains is a generic slice contains function.
Supports: bool, string, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64
slice := []string{"foo", "bar"}
v := "bar"
fmt.Println(slices.Contains(slice, v))
// Outputs: trueGreaterOf returns a greater function for the given slice type for slice sorting.
Supports: bool, string, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64
slice := []string{"foo", "bar"}
sort.Slice(slice, slices.GreaterOf(slice))
fmt.Println(slice)
// Outputs: [foo bar]LesserOf returns a lesser function for the given slice type for slice sorting.
Supports: bool, string, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64
slice := []string{"foo", "bar"}
sort.Slice(slice, slices.LesserOf(slice))
fmt.Println(slice)
// Outputs: [bar foo]BenchmarkContains-8 36659943 30.0 ns/op 0 B/op 0 allocs/op
BenchmarkContainsNative-8 48539482 24.9 ns/op 0 B/op 0 allocs/op
BenchmarkGreaterOf-8 6257299 193 ns/op 80 B/op 3 allocs/op
BenchmarkGreaterOfNative-8 7963461 149 ns/op 64 B/op 2 allocs/op
BenchmarkLesserOf-8 6122317 192 ns/op 80 B/op 3 allocs/op
BenchmarkLesserOfNative-8 7947242 152 ns/op 64 B/op 2 allocs/op
Always benchmark with your own workload. The result depends heavily on the data input.