這幾年json蠻流行的, golang當然也有提供json格式.
// hello46
package main
import (
"encoding/json"
"fmt"
)
type 秘技 struct {
Type string
Skill string
}
type 人物 struct {
Name string
Level string
Skills []*秘技
}
func main() {
sk1 := &秘技{"拳法", "伏虎拳"}
sk2 := &秘技{"刀法", "五虎斷門刀"}
茅十八 := 人物{"茅十八", "龍套", []*秘技{sk1, sk2}}
js, _ := json.Marshal(茅十八)
fmt.Printf("JSON format: %s\n", js)
}
執行結果:
λ ./hello46
JSON format: {"Name":"茅十八","Level":"龍套","Skills":[{"Type":"拳法","Skill":"伏虎拳"},{"Type":"刀法","Skill":"五虎斷門刀"}]}