type person struct {
name string
age int
}
//初始化
func main() {
var P person
P.name = "tom"
P.age = 25
fmt.Println(P.name)
P1 := person{"Tom1", 25}
fmt.Println(P1.name)
P2 := person{age: 24, name: "Tom"}
fmt.Println(P2.name)
}
type Human struct {
name string
age int
weight int
}
tyep Student struct {
Human //匿名字段,默认Student包含了Human的所有字段
speciality string
}
mark := Student(Human{"mark", 25, 120}, "Computer Science")
mark.name
mark.age
func (r ReciverType) funcName(params) (results) {
}
func (r *Rect) Area() float64 {
return r.width * r.height
}
func (b *Box) SetColor(c Color) {
b.color = c
}
type Human struct {
name string
}
type Student struct {
Human
School string
}
func (h *Human) SayHi() {
fmt.Println(h.name)
}
//则Student和Employee的实例可以调用
func main() {
h := Human{name: "human"}
fmt.Print(h.name)
h.SayHi()
s := Student{Human{"student"}}
s.SayHi()
}