go - Parsing integer from exec.Command output -
i'm trying run command inside go program , parse integer response. command returns output this:
6
fyb_src/ex1.fyb (1) wins!
splitting lines , removing whitespace strings.split
, strings.trimspaces
works fine. however, while trying parse number integer following error:
panic: strconv.parseint: parsing "0 \r1 \r2 \r3 \r4 \r5 \r6": invalid syntax
but printing string i'm trying parse terminal yields following result:
6
i'm not sure go here. number looks integer me. error message isn't useful either (at least me). have idea?
edit: code i'm running
out, _ := exec.command(pwd+"/osx_fukyobrane_amd64", "fyb_src/"+filename, "fyb_src/salty.fyb", "t").combinedoutput() parts := strings.split(string(out), "\n") fmt.println(parts[0]) rounds, err := strconv.atoi(strings.trimspace(parts[0])) if err != nil { panic(err.error()) }
it appear problem parts[0]
contains string "0 \r1 \r2 \r3 \r4 \r5 \r6"
instead of number.
the strconv.atoi function looking 1 number parse - assume trying first "0" in case. if so, problem code: strings.split(string(out), "\n")
looking "\n" not "\r", apparently being returned osx_fukyobrane_amd64
.
one solution instead split lines this:
regexp.mustcompile(`[\r\n]+`).split(parts[0], -1)
that collapse multiple lines 1 , treat \r, \n, , \r\n (or other weird combinations) valid line breaks.
Comments
Post a Comment