July 2019
Intermediate to advanced
458 pages
12h 12m
English
The first thing that can be improved is the relationship between arguments and spacing, by adding support for quoted strings. This can be done with bufio.Scanner with a custom split function that behaves like bufio.ScanWords apart from the fact that it's aware of quotes. The following code demonstrates this:
func ScanArgs(data []byte, atEOF bool) (advance int, token []byte, err error) { // first space start, first := 0, rune(0) for width := 0; start < len(data); start += width { first, width = utf8.DecodeRune(data[start:]) if !unicode.IsSpace(first) { break } } // skip quote if isQuote(first) { start++ }
The function has a first block that skips spaces and finds the first non-space character; if that character is a quote, ...
Read now
Unlock full access