Calling an external proces

Create the run.go file with the following content:

        package main

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

        func main() {

          prc := exec.Command("ls", "-a")
          out := bytes.NewBuffer([]byte{})
          prc.Stdout = out
          err := prc.Run()
          if err != nil {
            fmt.Println(err)
          }

          if prc.ProcessState.Success() {
            fmt.Println("Process run successfully with output:\n")
            fmt.Println(out.String())
          }
        }

output:

  Biradars-MacBook-Air-4:golang-daily sangam$ go run run.go
Process run successfully with output:

.
..
binary
main
main.go
run.go
test
util
  

## How it works…