I'm trying to parse a file with lines that consist of a key, a space, a number and then a newline.
My code works, but it doesn't smell right to me. Is there a better way to use Scanner? Particularly, I don't like having the Scan() inside the for-loop without any protection on it.
func TestScanner(t *testing.T) {
const input = `key1 62128128
key2 8337182720
key3 7834959872
key4 18001920
key5 593104896
`
scanner := bufio.NewScanner(strings.NewReader(input))
scanner.Split(bufio.ScanWords)
for scanner.Scan() {
key := scanner.Text()
scanner.Scan()
value := scanner.Text();
fmt.Printf("k: %v, v: %v
", key, value)
}
}