Skip to content

Instantly share code, notes, and snippets.

@hitechqb
Created September 15, 2019 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hitechqb/7c802e45144c2311e3858a2eafadf583 to your computer and use it in GitHub Desktop.
Save hitechqb/7c802e45144c2311e3858a2eafadf583 to your computer and use it in GitHub Desktop.
Using a Filename as an Input
func count(filename string) (int, error) {
file, err := os.Open(filename)
if err != nil {
return 0, errors.Wrapf(err, "unable to open %s", filename)
}
defer file.Close()
scanner := bufio.NewScanner(file)
count := 0
for scanner.Scan() {
if scanner.Text() == "" {
count++
}
}
return count, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment