I have the following struct representing a Webpage
type Webpage struct {
url.URL
references []url.URL
}
I want to represent a website as a collection of Webpages. I am using this struct, but it does not feel like what I am looking for:
type website struct {
[]Webpage
}
I read this as "a website has a slice of Webpages". I want a type that represents "a website is a slice of Webpages".
What type do I use to represent the is relationship instead of the has relationship of a struct field?
type Website []Webpage
Website
is slice of Webpage
Of course that's possible. Your structs should be like this.
type Webpage struct {
url url.URL
references []url.URL
}
type Website struct {
webpages []Webpage
}
So the Website
will contain a slice of Webpages
. This is common concept while handling with database. If you want to take more example and learn more about the concept you may take a look to this GORM Documentation
Hope it helps.
Edit:
On the book that you follow, there already provided example: https://www.golang-book.com/books/intro/9#section3
type MultiShape struct {
shapes []Shape
}