Skip to content

Instantly share code, notes, and snippets.

@FerdinaKusumah
Last active July 29, 2021 04:45
Show Gist options
  • Save FerdinaKusumah/ec8ffa11c5021e1e34f8ef72f1b994f2 to your computer and use it in GitHub Desktop.
Save FerdinaKusumah/ec8ffa11c5021e1e34f8ef72f1b994f2 to your computer and use it in GitHub Desktop.
Example Go Struct
package main
import (
"encoding/json"
"fmt"
)
type Hobby struct {
Name string `json:"name"`
}
type Student struct {
Name string `json:"name"`
Age int `json:"age"`
Hobby []Hobby `json:"hobby"`
}
func main() {
h := Hobby{}
h.Name = "swimming"
s := Student{}
s.Name = "John toer"
s.Age = 18
s.Hobby = []Hobby{h}
jsonStr, _ := json.Marshal(s)
fmt.Println(string(jsonStr))
// result is
// {"name":"John toer","age":18,"hobby":[{"name":"swimming"}]}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment