MOCKSTACKS
EN
Questions And Answers

More Tutorials








Go exec.Command

The Command returns the Cmd struct to execute the specified program with the given arguments. The first parameter is the program to be run; the other arguments are parameters to the program.

Example of Go Exec.Command

package main

import (
    "bytes"
    "fmt"
    "log"
    "os/exec"
    "strings"
)

func main() {
    cmd := exec.Command("tr", "a-z", "A-Z")

    cmd.Stdin = strings.NewReader("and old falcon")

    var out bytes.Buffer
    cmd.Stdout = &out

    err := cmd.Run()

    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("translated phrase: %q\n", out.String())
}



In the example, we transform the input via the tr command.

cmd := exec.Command("tr", "a-z", "A-Z")

The tr standard Linux command translates, squeezes, and/or deletes characters from standard input, writing to standard output. In our case, we transform lowercase letters to uppercase ones.

cmd.Stdin = strings.NewReader("and old falcon")

Through the Stdin field, we pass a string to the command as its input.

var out bytes.Buffer
cmd.Stdout = &out

The output of the program will be written to the bytes buffer.

$ go run command.go 
translated phrase: "AND OLD FALCON"



Go exec command with multiple args

We can pass multiple arguments to the exec.Command.

package main

import (
    "fmt"
    "os/exec"
)

func main() {

    prg := "echo"

    arg1 := "there"
    arg2 := "are three"
    arg3 := "falcons"

    cmd := exec.Command(prg, arg1, arg2, arg3)
    stdout, err := cmd.Output()

    if err != nil {
        fmt.Println(err.Error())
        return
    }

    fmt.Print(string(stdout))
}

The example runs the echo command with three arguments.

$ go run multiple_args.go 
there are three falcons


Go exec command capture output

The Output runs the command and returns its standard output.

package main

import (
    "fmt"
    "log"
    "os/exec"
)

func main() {

    out, err := exec.Command("ls", "-l").Output()

    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(string(out))
}

The example captures the output of the ls command and prints it.

Conclusion

In this page (written and validated by ) you learned about Go Exec Command . What's Next? If you are interested in completing Go tutorial, your next topic will be learning about: Go Pointers.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.