Skip to content

Instantly share code, notes, and snippets.

@arifsetyawan
Created December 29, 2019 07:44
Show Gist options
  • Save arifsetyawan/86386cbbe78bb8d0c6d0fb6bfb989ae7 to your computer and use it in GitHub Desktop.
Save arifsetyawan/86386cbbe78bb8d0c6d0fb6bfb989ae7 to your computer and use it in GitHub Desktop.
Json Marshal and Unmarshal
type User struct {
firstName string `json:"first_name"`
lastName string `json:"last_name"`
}
userObj = User{
firstName: "Arif",
lastName: "Setyawan"
}
// ENCODE =======================================
outputBytes, _ := json.Marshal(userObj);
fmt.Println(string(outputBytes)); // { "first_name": "Arif", "last_name": "Setyawan" }
// DECODE =======================================
var decodedUser User
err := json.Unmarshal(outputBytes, &decodedUser)
fmt.Printf("%+v", decodedUser)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment