I'm trying to accept a column separator for CSV parsing as a command line flag. Problem is the CSV library expects the separator to be a rune and not a string, which is giving me problems when I try to declare that a tab is the column separator.
If I hardcode a tab character into the code, Go correctly parses it as a single-rune string and returns the whole tab character as the rune. If I try to read it from the CLI flag, Go interprets it as a string containing a backslash and a 't', and only converts the backslash to a rune.
What do I need to do to get the whole tab character from the command line converted to a rune?
func processLine(line string) ([]string, error) {
strReader := strings.NewReader(line)
csvReader := csv.NewReader(strReader)
sepString := *separator // Declared like so: var separator = flag.String("separator", ",", "Single character to be used as a separator between fields")
fmt.Println("Separator is", string(sepString[0]))
fmt.Println("'", rune(sepString[0]), "'")
fmt.Println("'", string(rune("\t"[0])), "'")
csvReader.Comma = rune(sepString[0])
}
$ cat ~/T-609-group-names.csv | go run csvmaster.go --separator='\t'
====
P.S. - Yes, I could add some logic that specifically checks whether separator is a tab character and apply the hardcoded string then, but I'd prefer a less hacky, and more general, solution.