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 chapter04/recipe12.
  2. Navigate to the directory.
  3. Create the serialize.go file with the following content:
        package main        import (          "encoding/json"          "fmt"          "time"        )        func main() {          eur, err := time.LoadLocation("Europe/Vienna")          if err != nil {            panic(err)          }          t := time.Date(2017, 11, 20, 11, 20, 10, 0, eur)          // json.Marshaler interface          b, err := t.MarshalJSON()          if err != nil {            panic(err)          }          fmt.Println("Serialized as RFC 3339:", string(b))          t2 := time.Time{}          t2.UnmarshalJSON(b)          fmt.Println("Deserialized from RFC 3339:", t2)          // Serialize as epoch          epoch := t.Unix()          fmt.Println("Serialized as Epoch:", epoch)          // Deserialize epoch          jsonStr := fmt.Sprintf("{ \"created\":%d }", epoch)          data := struct { Created ...

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