Created
May 13, 2019 10:31
-
-
Save mjs/6dbdeaea8bd2e0df0abc295b6b1f3897 to your computer and use it in GitHub Desktop.
Demonstration of time marshalling and unmarshalling in Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output: