Skip to content

Instantly share code, notes, and snippets.

@mjs
Created May 13, 2019 10:31
Show Gist options
  • Save mjs/6dbdeaea8bd2e0df0abc295b6b1f3897 to your computer and use it in GitHub Desktop.
Save mjs/6dbdeaea8bd2e0df0abc295b6b1f3897 to your computer and use it in GitHub Desktop.
Demonstration of time marshalling and unmarshalling in Go
package main
import (
"fmt"
"time"
yaml "gopkg.in/yaml.v2"
)
type Data struct {
T time.Time
}
func main() {
d := &Data{time.Now().UTC()}
fmt.Printf(" initially: %+v\n", d)
m, err := yaml.Marshal(d)
if err != nil {
panic(err)
}
fmt.Printf(" marshalled: %q\n", string(m))
d2 := new(Data)
if err := yaml.Unmarshal(m, d2); err != nil {
panic(err)
}
fmt.Printf("unmarshalled: %+v\n", d2)
}
@mjs
Copy link
Author

mjs commented May 13, 2019

Example output:

$ go run main.go
   initially: &{T:2019-05-13 10:31:01.76718161 +0000 UTC}
  marshalled: "t: 2019-05-13T10:31:01.76718161Z\n"
unmarshalled: &{T:2019-05-13 10:31:01.76718161 +0000 UTC}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment