I'm stumped. I'm trying to write a custom
bufio.SplitFunc that can return an empty final token. Alas, my attempts oscillate between not returning the token at all and blowing up with a
100 empty tokens without progressing error. Consider the following basic code structure:
The MyScanLines function is copied almost verbatim from bufio.ScanLines. In this version, the string "foo\nbar\nbaz" (no trailing newline) correctly scans into three tokens. However, the string "foo\nbar\nbaz\n" (with a trailing newline) scans into those same three tokens when I in fact want the four tokens "foo", "bar", "baz", and "".
Removing the first if statement lets control flow to the return len(data), data, nil, which properly returns an empty slice, but because it consumes no data, the function gets called again and again and again with the same arguments.
I can think of some gross kludges to make MyScanLines work, for example using external state to hold the last token or returning a custom error type that embeds the final token. Nevertheless, I'd like to believe there's a clean, elegant way to return an empty final token from a bufio.SplitFunc. Is there?
Thanks,
— Scott