Golang Runs the Linux command

To Start the command, create a value of type exec.Cmd, then execute the Start method of that type, and get the output pipe of the command, which is of type IO.ReadCloser, and get the output through the pipe.

package main
import (

func main() {
  cmd0 := exec.Command("echo", "-n", "my first command")
  if err := cmd0.Start(); err != nil {
  fmt.Printf("command can not start %s \n", err)
  stdout0, err := cmd0.StdoutPipe()
  if err != nil {
  fmt.Printf("couldn't stdout pipe for command %s \n", err)
  var outputBuf0 bytes.Buffer
  for {
  tempOutput := make([]byte, 2048)
  n, err := stdout0.Read(tempOutput)
  if err != nil {
    if err == io.EOF {
    } else {
    fmt.Printf("couldn't read data from pip %s \n", err)
  if n > 0 {
  fmt.Printf("%s\n", outputBuf0.String())

Leave a Reply

Your email address will not be published. Required fields are marked *