Created
February 17, 2016 15:49
-
-
Save chespinoza/e93a132af504b71701cb to your computer and use it in GitHub Desktop.
Marshaling in go
This file contains 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 ( | |
"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