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 chapter06/recipe03.
  2. Navigate to the directory.
  3. Create the writefile.go file with the following content:
        package main        import (          "io"          "os"          "strings"        )        func main() {          f, err := os.Create("sample.file")          if err != nil {            panic(err)          }          defer f.Close()          _, err = f.WriteString("Go is awesome!\n")          if err != nil {            panic(err)          }          _, err = io.Copy(f, strings.NewReader("Yeah! Go                            is great.\n"))          if err != nil {            panic(err)          }        }
  1. Execute the code by running go run writefile.go in the main Terminal.
  2. Check the content of the created sample.file:

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