golang strcut 初始化

package main



import (
“fmt”
“encoding/json”
)



type StructA struct{
A int json:"a"
B string json:"b"
}



type structB struct{
StructA
C string json:"c"
}



type structC struct{
A int json:"a"
B string json:"b"
C string json:"c"
}



func main() {
fmt.Println(structB{})
fmt.Println(structC{})
fmt.Println(structB{C:”3”})
//fmt.Println(structB{A:1,B:”2”,C:”3”})
//fmt.Println(structB{{A:1,B:”2”},C:”3”})
//fmt.Println(structB{{1,”2”},C:”3”})
//fmt.Println(structB{{1,”2”},”3”})
fmt.Println(structB{StructA{1,”2”},”3”})
//fmt.Println(structB{StructA{1,”2”},C:”3”})
//fmt.Println(structB{StructA{A:1,B:”2”},C:”3”})
fmt.Println(structB{StructA{A:1,B:”2”},”3”})
//如果有包名,外面不带,里面带
fmt.Println(structB{StructA:StructA{A:1,B:”2”},C:”3”})
d,e:=json.Marshal(structB{StructA:StructA{A:1,B:”2”},C:”3”})
fmt.Println(string(d),e)
var c structC
err1:=json.Unmarshal(d,&c)
fmt.Println(c,err1)
//var f structB=structB(c)
var f structB
err2:=json.Unmarshal(d,&f)
fmt.Println(f,err2)
}

{{0 } }
{0 }
{{0 } 3}
{{1 2} 3}
{{1 2} 3}
{{1 2} 3}
{“a”:1,”b”:”2”,”c”:”3”}
{1 2 3}
{\{1 2} 3}


Category golang