Exec is a subpackage in the os package that can be used to run external commands using Go. The Go exec command tutorial shows how to execute shell commands and programs in Golang.
To use this package, we need to import it as follows:
import "os/exec"
Run commands using the GoLang exec package
We can run any command we want. Just like we use CMD, bash, or some other shell to run commands, it can run those commands.
Here is an example of running the ls command. Create a new one:
package main import ( "fmt" "os/exec" ) func main() { cmd := ("ls") e := () CheckError(e) } func CheckError(e error) { if e != nil { (e) } }
The Run function starts the specified command and waits for it to complete, while Start starts the specified command but does not wait for it to complete; we need to use Wait with Start.
Then create a new file:
$ go mod init go: creating new : module go: to add module requirements and sums: go mod tidy
Now the program will run, but we won't see any output from the console. The reason is that the command is running and the output is not sent to the standard output.
$ go run
So, we need to fix it. Add the two lines shown below to see any output from the console.
= =
The output will display the files in the current directory.
package main import ( "fmt" "os" "os/exec" ) func main() { cmd := ("ls", "-lah") = = e := () CheckError(e) } func CheckError(e error) { if e != nil { (e) } }
Then we can see the following file output from the standard station:
$ go run total 16 drwxr-xr-x 4 yuzhou_1su staff 128B 5 15 22:56 . drwxr-xr-x 23 yuzhou_1su staff 736B 5 15 22:53 .. -rw-r--r-- 1 yuzhou_1su staff 24B 5 15 22:56 -rw-r--r-- 1 yuzhou_1su staff 248B 5 15 23:18
By directly running the command with direct ls, you can see that the result is correct:
$ ls -alh total 16 drwxr-xr-x 4 yuzhou_1su staff 128B 5 15 22:56 . drwxr-xr-x 23 yuzhou_1su staff 736B 5 15 22:53 .. -rw-r--r-- 1 yuzhou_1su staff 24B 5 15 22:56 -rw-r--r-- 1 yuzhou_1su staff 248B 5 15 23:18
Specify commands for different operating systems
We can specify that different commands run for different operating systems (such as bash commands on Linux). Here is an example.
if == "linux" { cmd = ("ls") }
To do this, we also need to import the runtime package.
To see all possible operating systems, we can run go tool dist list , which will display all possible operating systems and ARCH combinations.
Go exec command captures output
Output the run command and return its standard output:
package main import ( "fmt" "log" "os/exec" ) func main() { out, err := ("ls", "-l").Output() if err != nil { (err) } (string(out)) }
Run the program:
$ go run total 16 -rw-r--r-- 1 yuzhou_1su staff 24 5 15 22:56 -rw-r--r-- 1 yuzhou_1su staff 180 5 15 23:33
Go
The pipeline allows us to send the output of one command to another. StdinPipe returns a pipeline that will be connected to the command's standard input when the command starts.
package main import ( "fmt" "io" "log" "os/exec" ) func main() { cmd := ("cat") stdin, err := () if err != nil { (err) } go func() { defer () (stdin, "an old falcon") }() out, err := () if err != nil { (err) } ("%s\n", out) }
In the code example, we write the string to standard input within goroutine.
cmd := ("cat")
The cat command connects the given file to standard output. When there is no given file or with -, the command reads the standard input and prints it to the standard output.
stdin, err := ()
We get the standard input pipeline for the cat command.
go func() { defer () (stdin, "an old falcon") }()
Inside goroutine we write a string to the standard input pipeline.
$ go run an old falcon
Go
StdoutPipe Returns a pipeline that will be connected to the command's standard output when the command starts.
package main import ( "fmt" "io/ioutil" "log" "os/exec" "strings" ) func upper(data string) string { return (data) } func main() { cmd := ("echo", "an old falcon") stdout, err := () if err != nil { (err) } if err := (); err != nil { (err) } data, err := (stdout) if err != nil { (err) } if err := (); err != nil { (err) } ("%s\n", upper(string(data))) }
This example reads the output of the echo command through a pipeline and converts it to capital letters.
cmd := ("echo", "an old falcon")
The command to be run is the echo command with a single string parameter.
stdout, err := ()
We get the standard output pipeline.
if err := (); err != nil { (err) }
The command is executed using the Start function; it will not wait for it to complete.
data, err := (stdout)
We read data from the pipeline.
if err := (); err != nil { (err) }
Wait waits for the command to exit and waits for any copying to or from stdout or stderr to complete. It closes the pipeline after seeing the command exit.
Run the program:
$ go run AN OLD FALCON
Summarize
The os/exec package runs external commands. It wraps it to make it easier to remap standard input and standard output, connect I/O to pipeline, and make other adjustments.
Reference link:
/golang/exec-command/
The above is the detailed content of the basic usage example of the introduction to Go language exec. For more information about the use cases of Go language exec, please pay attention to my other related articles!