Skip to content

Instantly share code, notes, and snippets.

@chespinoza
Created February 17, 2016 15:49
Show Gist options
  • Save chespinoza/e93a132af504b71701cb to your computer and use it in GitHub Desktop.
Save chespinoza/e93a132af504b71701cb to your computer and use it in GitHub Desktop.
Marshaling in go
package main
import (
"encoding/json"
"fmt"
"log"
"time"
)
type User struct {
Id int64
Name string
LastName string
CFK int64
Card CreditCard
}
type CreditCard struct {
Serial int64
Name string
Until time.Time
Code string
}
func main() {
//JSON ENCODE
user := User{
Id: 1,
Name: "Peter",
LastName: "Preek",
CFK: 12,
Card: CreditCard{
Serial: 1212871265128712,
Name: "Peter Preek",
Until: time.Now(),
Code: "064",
},
}
u, err := json.Marshal(user)
if err != nil {
log.Fatal(err)
}
fmt.Printf("json string:%s\n", u)
//JSON DECODE
var oldUser User
err = json.Unmarshal(u, &oldUser)
fmt.Printf("%v", oldUser)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment