Newer
Older
01_hello / struct / anonymousField.go
yhornisse on 25 Sep 2021 589 bytes add struct sample
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} }
}