Created
March 26, 2018 08:05
-
-
Save danrl/eb718ae97e93b95452f42161da8fb090 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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