Skip to content

Instantly share code, notes, and snippets.

@danrl
Created March 26, 2018 08:05
Show Gist options
  • Save danrl/eb718ae97e93b95452f42161da8fb090 to your computer and use it in GitHub Desktop.
Save danrl/eb718ae97e93b95452f42161da8fb090 to your computer and use it in GitHub Desktop.
func reader(fname string, out chan<- *line) {
defer close(out) // close channel on return
// open the file
file, err := os.Open(fname)
if err != nil {
log.Fatalf("open: %v", err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
header := true
for scanner.Scan() {
var l line
columns := strings.SplitN(scanner.Text(), ",", 2)
// ignore first line (header)
if header {
header = false
continue
}
// convert ID to integer for easier comparison
id, err := strconv.Atoi(columns[0])
if err != nil {
log.Fatalf("ParseInt: %v", err)
}
l.id = id
l.restOfLine = columns[1]
// send the line to the channel
out <- &l
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment