How do you get the output of a system command in Go?

There is an easier way now:

package main

import (

func main() {
    out, err := exec.Command("date").Output()
    if err != nil {
    fmt.Printf("The date is %s\n", out)

Where out is the standard output. It’s in the format []byte, but you can change it to string easily with:


You can also use CombinedOutput() instead of Output() which returns standard output and standard error.


Leave a Comment