t := "haystack needle haystack" // "text" - thing we search in p := "needle" // "pattern" - thing we search for func naive(p, t string) []int { // assume len(p) <= len(t) occurrences := []int{} for i := 0; i < len(t) - len(p) + 1; i++ { // for each alignment of p to t match := true // assume we match until proven wrong for j := 0; j < len(p); j++ { // for each position of p if t[i+j] != p[j] { match = false // at least 1 char mismatches break } } if match { occurrences = append(occurrences, i) } } return occurrences } naive(p, t) t[9:9+len(p)] naive("needle", "needleneedleneedle")