Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Created June 29, 2018 01:28
Show Gist options
  • Save miguelmota/82edbee6464c7669bd051332dc91695e to your computer and use it in GitHub Desktop.
Save miguelmota/82edbee6464c7669bd051332dc91695e to your computer and use it in GitHub Desktop.
Golang execute system shell command
package main
import (
"fmt"
"os/exec"
"strings"
)
func hello() string {
cmd := "echo hello"
out, err := exec.Command("sh", "-c", cmd).Output()
if err != nil {
return ""
}
return strings.TrimSpace(string(out))
}
func main() {
fmt.Println(hello())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment