Skip to Main Content
Go Standard Library Cookbook
book

Go Standard Library Cookbook

by Radomír Sohlich
February 2018
Intermediate to advanced content levelIntermediate to advanced
340 pages
9h 43m
English
Packt Publishing
Content preview from Go Standard Library Cookbook

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: ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Go Systems Programming

Go Systems Programming

Mihalis Tsoukalos
Go: Building Web Applications

Go: Building Web Applications

Nathan Kozyra, Mat Ryer

Publisher Resources

ISBN: 9781788475273Supplemental Content