go – How wrap a command that is interactive-ThrowExceptions

Exception or error:

I am building an ftp wrapper that does some stuff before I spawn, I could easily do it in a shell script but wondering how I could do it in go

While exec.Command works for simple commands.

out, err := exec.Command("ls").Output() // Works

How do I wrap commands that are interactive e.g., ftp

out, err := exec.Command("ftp").Output()

It just exits. How do I deal with stdin ?

e.g., bash equivalent :

> ./t.sh 
Welcome to myftp 

ftp> open blahblah.com

> cat t.sh 
echo "Welcome to myftp "
#extra commands such as auth/authoriz.. etc.,
echo "$@"

c++ equivalent :

int main() {
    return 0;
How to solve:

I would probably do something like this which is more native and doesn’t involve an external package

package main
import (

func main() {

    cmd := exec.Command("ls")
    // redirect the output to terminal
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr




Traditionally these sorts of interactive scripting exercises are best done with expect. May I suggest checking out a pure Go equivalent?

From the readme:

child, err := gexpect.Spawn("python")
if err != nil { panic(err) }
child.SendLine("print 'Hello World'")


To run any commands which will prompt for user input:

cmd := exec.Command("cf", "login")
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin


Leave a Reply

Your email address will not be published. Required fields are marked *