didyoumean
is a Go package that suggests the most likely string based on the Levenshtein distance algorithm. It is particularly useful for correcting user typos in command-line tools or offering suggestions in search inputs.
You need to have Go 1.15 or higher installed on your machine.
To use didyoumean
in your Go project, run:
go get -u github.com/AL-Kost/didyoumean
This will retrieve the library and add it to your project's dependencies.
Import DidYouMean in your Go code:
import "github.com/AL-Kost/didyoumean"
Example of suggesting a string:
package main
import (
"fmt"
"github.com/AL-Kost/didyoumean"
)
func main() {
input := "appl"
candidates := []string{"apple", "application", "apology", "apply", "happy"}
threshold := 2
caseInsensitive := true
closestMatch := didyoumean.Suggest(input, candidates, threshold, caseInsensitive)
if closestMatch != "" {
fmt.Printf("Did you mean '%s'?\n", closestMatch)
} else {
fmt.Println("No close match found.")
}
}
For more examples, please refer to the examples
directory.