1

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.

Rahul Patil
  • 2,707
  • 2
  • 21
  • 32
  • You mean something like this: https://stackoverflow.com/questions/10473800/in-go-how-do-i-capture-stdout-of-a-function-into-a-string? It allows to capture also stdout of command and whatnot – xReprisal Oct 22 '18 at 07:47
  • I mean like a binary on executing asks for your name and prints `hello name`. Here, the above program doesn't ask for input and prints hello and ends. SO how to get the input for name? – Jessica Stu Oct 22 '18 at 07:51
  • You can either pass a command line argument and read it inside the child process with the `flag` package, or if you want dynamic, interactive input, you can hook into the child process' stdin, see: https://golang.org/pkg/os/exec/#example_Cmd_StdinPipe – Not_a_Golfer Oct 22 '18 at 07:54
  • @Not_a_Golfer I tried the given solution its not working and the programs are going to dynamic so it has to be interactive. Thanks anyway! – Jessica Stu Oct 22 '18 at 08:12
  • I don't get your exact problem. Do you want to connect your stdin to the command's stdin while the command is running? – Arman Ordookhani Oct 22 '18 at 10:50

1 Answers1

0

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('\n')
    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]))
}
xReprisal
  • 810
  • 8
  • 23
  • its not just about `hello name` . I mean taking input for any executable. – Jessica Stu Oct 22 '18 at 08:45
  • You could copy/iterate over code that takes input as many times as you wish. – xReprisal Oct 22 '18 at 08:58
  • that's the thing, I won't know how many time the running program is gonna take input Maybe it is 0 times if it just shows some detail fetched from a page and maybe 100, 1000 times. Thats the why i want that the control is fully transferred to that executable and transferred back when its execution is complete. – Jessica Stu Oct 22 '18 at 09:38
  • Sorry, I've finally understood the issue. I'm gonna tinker with it a bit after work if nobody comes up with answer before that. – xReprisal Oct 22 '18 at 09:44