Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling an external command in Go

Tags:

go

How can I call an external command in GO? I need to call an external program and wait for it to finish execution. before the next statement is executed.

like image 445
vishnu Avatar asked Jan 12 '23 13:01

vishnu


1 Answers

You need to use the exec package : start a command using Command and use Run to wait for completion.

cmd := exec.Command("yourcommand", "some", "args")
if err := cmd.Run(); err != nil { 
    fmt.Println("Error: ", err)
}   

If you just want to read the result, you may use Output instead of Run.

like image 67
Denys Séguret Avatar answered Jan 30 '23 20:01

Denys Séguret