结构体入门
hello world
结构体创建
type Book struct {
Title string
Author string
Year int
CheckedOut bool
}
func main() {
b1 := Book{}
info := []byte(`{"Title":"a","Author":"b", "Year": 1, "CheckedOut": false}`)
_ = json.Unmarshal(info, &b1)
fmt.Printf("book title is %s", b1.Title)
}
const myData = `{
"name":"alex",
"age":3012312123423423421
}`
type myObj struct {
Age int64 `json:"age"`
}
func main() {
//obj := make(map[string]interface{})
obj := myObj{}
_ = json.Unmarshal([]byte(myData), &obj)
data, _ := json.Marshal(obj)
fmt.Println(string(data))
}
结构体方法
值接收器和指针接收器
Last updated