diff --git a/struct/anonymousField.go b/struct/anonymousField.go new file mode 100644 index 0000000..3e26890 --- /dev/null +++ b/struct/anonymousField.go @@ -0,0 +1,36 @@ +package main + +import "fmt" + +type SampleStruct1 struct { + Id int64 + Name string +} + +type SampleStruct2 struct { + SampleStruct1 + Address string +} + + +type SampleStruct3 struct { + SampleStruct1 + // SampleStruct1 // NG! + Address string +} + + +func main() { + v1 := SampleStruct2{SampleStruct1{1, "taro"}, "address"} + fmt.Println(v1) // {{1 taro} address} + + v2 := SampleStruct2{} + fmt.Println(v2) // {{0 } } + v2.Id = 2 + v2.Name = "jiro" + v2.Address = "address2" + fmt.Println(v2) // {{2 jiro} address2} + + v3 := SampleStruct2{ SampleStruct1: SampleStruct1{1, "taro"} } + fmt.Println(v3) // {{1 taro} } +}