Created
June 27, 2015 02:14
-
-
Save heshed/f357781b9fa22ee6f04e to your computer and use it in GitHub Desktop.
read next line from file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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