There are two ways to initialize a struct in **golang: 1. Declare the key of all fields, 2. Neither declares key, but value corresponds to the order one by one defined.
Although mode 1 is cumbersome, anonymous fields may not have to be initialized when there is an anonymous field in the defined struct, and the anonymous field needs to be initialized in mode 2, otherwise the compiler will error (too few values in struct initializer).
"Subclass" in Golang can "inherit" the method defined at the previous level, which is equivalent to the overloaded concept in OOP * *
Package main
Import (
"FMT"
)
type language []string
type human struct {
name string
age int
sex bool
}
type programmer struct {
human company
string
language
}
func (H *human) introduce () {
fmt. PRINTLN ("Human")
}
func (P *programmer) introduce () {
fmt. PRINTLN ("Programmer's")
}
func main () {
// h: = human{name: "SDA", Age:123, Sex:true}
P: = programmer{ Human:human{name: "SDA", Age:123, Sex:true}, Company: "Iflytek"}
p.introduce ()
p.human.introduce ()
}