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/recipe10.
  2. Navigate to the directory.
  3. Create the gob.go file with the following content:
        package main        import (          "bytes"          "encoding/gob"          "fmt"        )        type User struct {          FirstName string          LastName string          Age int          Active bool        }        func (u User) String() string {          return fmt.Sprintf(`{"FirstName":%s,"LastName":%s,                               "Age":%d,"Active":%v }`,          u.FirstName, u.LastName, u.Age, u.Active)        }        type SimpleUser struct {          FirstName string          LastName string        }        func (u SimpleUser) String() string {          return fmt.Sprintf(`{"FirstName":%s,"LastName":%s}`,          u.FirstName, u.LastName)        }        func main() {          var buff bytes.Buffer          // Encode value          enc := gob.NewEncoder(&buff)          user := User{            "Radomir",            "Sohlich",            30,            true,          } enc.Encode(user) ...

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