package main
import "fmt"
import "encoding/json"
type address struct{
city string
}
type person struct{
string
int
address
}
type Animal struct{
name string
}
func (a *Animal)move(){
fmt.Printf("%s 会动\n", a.name)
}
type Dog struct{
Feet int8
*Animal
}
type class struct{
Title string
Students []string
}
func main() {
p1 := person{
"小王子",
18,
address{
"上海",
},
}
fmt.Println(p1)
fmt.Println(p1.string)
fmt.Println(p1.address.city)
fmt.Println(p1.city)
d1 := Dog{
4,
&Animal{
"贝贝",
},
}
fmt.Println(d1)
d1.move()
c1 := class{
Title: "三年二班",
Students: []string{"李红", "李白"},
}
fmt.Println(c1)
data, err := json.Marshal(c1)
fmt.Println(string(data))
fmt.Println(err)
jsonStr := "{\"Title\":\"三年二班\",\"Students\":[\"李红\",\"李白\"]}"
var c2 class
err2 := json.Unmarshal( []byte(jsonStr), &c2)
fmt.Println(c2)
fmt.Println(err2)
}
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步