Skip to content

Instantly share code, notes, and snippets.

@heshed
Created June 27, 2015 02:14
Show Gist options
  • Save heshed/f357781b9fa22ee6f04e to your computer and use it in GitHub Desktop.
Save heshed/f357781b9fa22ee6f04e to your computer and use it in GitHub Desktop.
read next line from file
import (
"bufio"
"os"
)
type Reader struct {
path string
input *os.File
scanner *bufio.Scanner
}
func NewReader(path string) *Reader {
input, err := os.Open(path)
if err != nil {
panic(err)
}
return &Reader{
path: path,
input: input,
scanner: bufio.NewScanner(input),
}
}
func (r *Reader) Next() (string, bool) {
if r.scanner.Scan() {
return r.scanner.Text(), true
} else {
r.input.Close()
return "", false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment