I am using below code snippet :-
command:= exec.Command("./"+order)
out, err := command.Output()
if err != nil {
log.Println(err)
}
fmt.Println(string(out))
here, "order" is the variable with name of binary in the current directory. When I run this code it doesn't ask for input and run through the binary till the end of it printing output statement. How do I get input for my binary executable while running it?
I have tried using python in go also but to no effect.
This code asks for your name and then passes it to ./hello
binary which reads first argument and outputs the first argument.
package main
import (
"bufio"
"fmt"
"log"
"os"
"os/exec"
)
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("What's your name: ")
name, _ := reader.ReadString('
')
command := exec.Command("./hello", name)
out, err := command.Output()
if err != nil {
log.Println(err)
}
fmt.Println(string(out))
}
hello.go:
package main
import (
"fmt"
"os"
)
func main() {
fmt.Println(fmt.Sprintf("hello %s", os.Args[1]))
}