Skip to content

Instantly share code, notes, and snippets.

@smagch
Created May 12, 2014 18:07
Show Gist options
  • Select an option

  • Save smagch/1b4d2676cf5f9d4c5068 to your computer and use it in GitHub Desktop.

Select an option

Save smagch/1b4d2676cf5f9d4c5068 to your computer and use it in GitHub Desktop.
JSON
package main
import (
"encoding/json"
"fmt"
)
type Post struct {
Id int `json:"id"`
UserId int `json:"-"`
Title string `json:"title"`
}
type User struct {
Id int `json:"id"`
Name string `json:"name"`
}
type J struct {
*Post
User *User `json:"user"`
}
func main() {
p := &Post{100, 1, "foobar2000"}
u := &User{1, "tomoya"}
j := &J{p, u}
b, err := json.Marshal(j)
if err != nil {
panic(err)
}
fmt.Println(string(b))
//fmt.Println("Hello, playground", p, u, j)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment