Last active
July 29, 2021 04:45
-
-
Save FerdinaKusumah/ec8ffa11c5021e1e34f8ef72f1b994f2 to your computer and use it in GitHub Desktop.
Example Go Struct
This file contains hidden or 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" | |
) | |
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