点击运行
package main import ( "fmt" ) type author struct { firstName string lastName string bio string } func (a author) fullName() string { return fmt.Sprintf("%s %s", a.firstName, a.lastName) } type blogPost struct { title string content string author } func (b blogPost) details() { fmt.Println("Title: ", b.title) fmt.Println("Content: ", b.content) fmt.Println("Author: ", b.fullName()) fmt.Println("Bio: ", b.bio) } func main() { author1 := author{ "Naveen", "Ramanathan", "Golang Enthusiast", } blogPost1 := blogPost{ "Inheritance in Go", "Go supports composition instead of inheritance", author1, } blogPost1.details() }
运行结果 :
正在执行...