4.22.2 ارث بری

4.22.2 ارث بری

در زبان گو ارث بری در واقع type embedding هست با استفاده type embedding می توانید یک type را داخل type دیگری جاسازی کنید و به واسطه type والد قابل دسترس است.

package main

import "fmt"

type Person struct {
	Name string
}

func (p *Person) Introduce() {
	fmt.Printf("Hi, my name is %s\n", p.Name)
}

type Student struct {
	Person
	School string
}

func main() {
	s := &Student{Person{"John Doe"}, "Go University"}
	s.Introduce()
}