Reputation: 473
I'm creating a new command with exec, obtaining the Stdin io.WriteCloser
and writing to it as follows:
cmd := exec.Command(flag.Arg(0), flag.Args()[1:]...)
cmdInWriter, err := cmd.StdinPipe()
err = cmd.Start()
go func() {
for {
var c string
_, err = fmt.Scanln(&c)
written, err := io.WriteString(cmdInWriter, c)
fmt.Println(written) // prints 4, if c is "help"
}
}()
However, this doesn't seem to actually write to the program.
flag.Arg(0)
= javaflag.Args()[1:]...
= an array of arguments passed to the java programI tested it with another program I quickly made (the previous list does not apply there) and the string was again not being written. What am I doing wrong?
Upvotes: 0
Views: 777
Reputation: 51567
You are missing the linefeed. When you read a string using Scanln, the resulting string does not have the linefeed at the end:
written, err := fmt.Fprintln(cmdInWriter, c)
Upvotes: 2