TheAlgorithms/Go · warning
pattern was not found in the input string
Error message
pattern was not found in the input string
What it means
horspool.ErrNotFound is the sentinel error returned by the Boyer–Moore–Horspool search (Horspool) when the pattern does not occur in the input string. It is also reused by other search helpers (e.g. math/kthnumber.go) as a generic 'not found' sentinel when an index is out of range or no result exists.
Source
Thrown at strings/horspool/horspool.go:8
// Implementation of the
// [Boyer–Moore–Horspool algorithm](https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore%E2%80%93Horspool_algorithm)
package horspool
import "errors"
var ErrNotFound = errors.New("pattern was not found in the input string")
func Horspool(t, p string) (int, error) {
// in order to handle multy-byte character properly
// the input is converted into rune arrays
return horspool([]rune(t), []rune(p))
}
func horspool(t, p []rune) (int, error) {
shiftMap := computeShiftMap(t, p)
pos := 0
for pos <= len(t)-len(p) {
if isMatch(pos, t, p) {
return pos, nil
}
if pos+len(p) >= len(t) {
// because the remaining length of the input string
// is the same as the length of the pattern
// and it does not match the patternView on GitHub (pinned to 5ba447ec5f)
Solutions
- Treat the returned error as an expected outcome: check errors.Is(err, horspool.ErrNotFound) and handle the miss case
- Validate k against len(nums) (0 <= k < len) before calling kthNumber
- Verify the pattern and haystack are non-empty and correct before searching
Example fix
// before
idx, err := horspool.Horspool(text, pattern) // unhandled
// after
idx, err := horspool.Horspool(text, pattern)
if errors.Is(err, horspool.ErrNotFound) { /* pattern absent — handle */ } Defensive patterns
Strategy: try-catch
Validate before calling
if k < 0 || k >= len(nums) {
// out-of-range: would yield ErrNotFound
}
if pattern == "" || text == "" {
// empty inputs: pattern cannot be found
} Try / catch
idx, err := horspool.Horspool(text, pattern)
if errors.Is(err, horspool.ErrNotFound) {
// not-found is an expected result, not a failure
return -1 // or sentinel of your own
} else if err != nil {
return err
} Prevention
- Always use errors.Is against the sentinel, never string comparison
- Clamp k to [0, len(nums)-1] before kthNumber
- Check for empty pattern/haystack before searching
When it happens
Trigger: Calling horspool.Horspool(t, p) where p never appears in t; calling kthNumber(nums, k) with k < 0 or k >= len(nums), or when the search space is exhausted without finding the kth value.
Common situations: Searching user-provided text for a pattern that may legitimately be absent; off-by-one k values (k is zero-based) when selecting the kth smallest/largest number; empty input slices or strings.
Related errors
AI-assisted analysis of TheAlgorithms/Go@5ba447ec5f (2026-09-02).
Data as JSON: /api/errors/29b79a46933fbd69.
Report an issue: GitHub.