Golang的繼承可以通過結構體裏面包含匿名結構體實現,具體,比如iPhone這個結構體要繼承法phone這個結構體可以這樣寫:
package main
import "fmt"
type phone struct {
design_place string
production_place string
}
type iphone struct {
brand string
phone
}
func main() {
thePhone := phone{
design_place: "California",
production_place: "China",
}
thisPhone := iphone{
brand: "Apple",
phone: thePhone,
}
fmt.Println(thisPhone.production_place, thisPhone.brand)
}