Use scanner to iterate over the file, line-by-line, incrementing your line count on each loop.
e.g.
f, err := os.Open(path)
if err != nil {
return 0, err
}
defer f.Close()
// Splits on newlines by default.
scanner := bufio.NewScanner(f)
line := 1
// https://golang.org/pkg/bufio/#Scanner.Scan
for scanner.Scan() {
if strings.Contains(scanner.Text(), "yourstring") {
return line, nil
}
line++
}
if err := scanner.Err(); err != nil {
// Handle the error
}
Update: if you need to do this across 'thousands of files' (as per the comment on another answer), then you would wrap this approach in a worker pool and run this concurrently.