Use Kwker from Go
The Go package kwker.io/go/kwker calls the C library through cgo, so it needs the C package and a C compiler. Slices
of every integer width and both float widths are sorted in place, without copies.
Setup
Install the C package for your platform first (Installation), then, with Go 1.25 or later:
go mod init latency
go get kwker.io/go/kwker
The Go command finds the C package through pkg-config. If you unpacked the archive outside the standard places, set
PKG_CONFIG_PATH to its lib/pkgconfig directory.
The program
The three slowest requests, without sorting the whole slice (main.go):
package main
import (
"fmt"
"kwker.io/go/kwker"
)
func main() {
latencyMs := []float64{12.1, 250.4, 8.9, 97.0, 310.2, 15.5, 260.0}
values, positions := kwker.TopK(latencyMs, 3, kwker.Descending, true)
fmt.Println(values)
fmt.Println(positions)
}
Output
[310.2 260 250.4] [4 6 1]
Build and run
go run .
Errors
Invalid arguments panic, as an out-of-range slice index does. SortFile, SetISA and Observe return an error.
Next steps
- Go API reference: every function, its arguments and errors.
- Quickstart: Kwker Core: sorting, order and medians in every language.