Hi.
I’ve a very simple go program - input-tester.go - that takes a string containing new lines as a parameter and splits the string on the new line:
package main
import (
"os"
"fmt"
"strings"
)
func main() {
if len(os.Args) == 2 {
strArray := strings.Split(os.Args[1], "\n")
fmt.Println("len:", len(strArray))
for i, s := range strArray {
fmt.Printf("%d:%s\n", i,s)
}
}
}
I’ve got another go program - executor.go - which executes input-tester:
package main
import (
"os/exec"
"bytes"
"fmt"
)
func main() {
parameter := "col1,col2,col3\nval1,val2,val3"
cmd := exec.Command("/path/to/package/input-tester", parameter)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
panic(err)
}
fmt.Println(cmd.Stdout)
}
If I run the executor my output is:
len: 2
0:col1,col2,col3
1:val1,val2,val3
However if I run the input-tester.go from the command line (go run input-tester.go col1,col2,col3\nval1,val2,val3) the output is:
len: 1
0:col1,col2,col3\nval1,val2,val3
I understand that the issue is the difference between interpreted string from the executor and raw string from the command line. I also know if I update the strings.Split to use backticks n
the result gets swapped around.
My question is - is there a way to make this work consistantly using the command line and the executor? Is there a way to change an interpreted string to a raw string or vice versa? I’ve checked the the utf8, strings and strconv packages with not much luck.
Thanks in advance.