O'Reilly logo

Go Standard Library Cookbook by Radomir Sohlich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it…

  1. Open the console and create the folder chapter01/recipe10.
  2. Navigate to the directory.
  3. Create the main_read_output.go file with the following content:
       package main       import (         "fmt"         "os/exec"         "runtime"       )       func main() {         var cmd string         if runtime.GOOS == "windows" {           cmd = "dir"         } else {           cmd = "ls"         }         proc := exec.Command(cmd)         // Output will run the process         // terminates and returns the standard         // output in a byte slice.         buff, err := proc.Output()         if err != nil {           panic(err)         }         // The output of child         // process in form         // of byte slice         // printed as string         fmt.Println(string(buff))       }
  1. Run the code by executing go run main_read_output.go.
  2. See the output in the Terminal:
  1. Create the main_read_stdout.go file with the following content: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required