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/recipe07.
  2. Navigate to the directory.
  3. Create the rwbinary.go file with the following content:
        package main        import (          "bytes"          "encoding/binary"          "fmt"        )        func main() {          // Writing binary values          buf := bytes.NewBuffer([]byte{})          if err := binary.Write(buf, binary.BigEndian, 1.004);           err != nil {            panic(err)          }          if err := binary.Write(buf, binary.BigEndian,                   []byte("Hello")); err != nil {            panic(err)          }          // Reading the written values          var num float64          if err := binary.Read(buf, binary.BigEndian, &num);           err != nil {            panic(err)          }          fmt.Printf("float64: %.3f\n", num)          greeting := make([]byte, 5)          if err := binary.Read(buf, binary.BigEndian, &greeting);          err != nil {            panic(err)          } fmt.Printf("string: ...

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