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/recipe04.
  2. Navigate to the directory.
  3. Create the directory temp and create the file file.txt in it.
  4. Edit the file.txt file and write multiple lines into the file.
  1. Create the readfile.go file with the following content:
        package main        import "os"        import "bufio"        import "bytes"        import "fmt"        import "io/ioutil"        func main() {          fmt.Println("### Read as reader ###")          f, err := os.Open("temp/file.txt")          if err != nil {            panic(err)          }          defer f.Close()          // Read the          // file with reader          wr := bytes.Buffer{}          sc := bufio.NewScanner(f)          for sc.Scan() {            wr.WriteString(sc.Text())          }          fmt.Println(wr.String())          fmt.Println("### ReadFile ###")          // for smaller files fContent, err := ioutil.ReadFile("temp/file.txt") ...

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