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 chapter05/recipe02.
  2. Navigate to the directory.
  3. Create the stdouterr.go file with the following content:
        package main        import (          "fmt"          "io"          "os"         )         func main() {           // Simply write string           io.WriteString(os.Stdout,           "This is string to standard output.\n")           io.WriteString(os.Stderr,           "This is string to standard error output.\n")           // Stdout/err implements           // writer interface           buf := []byte{0xAF, 0xFF, 0xFE}           for i := 0; i < 200; i++ {             if _, e := os.Stdout.Write(buf); e != nil {               panic(e)             }           }           // The fmt package           // could be used too           fmt.Fprintln(os.Stdout, "\n")         }
  1. Execute the code with go run stdouterr.go.
  1. See the output:

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