How to initialize inherited object's fields
go
Solution
In Go, these fields from embedded structs are called promoted fields.
The Go Specification states (my emphasis):
Promoted fields act like ordinary fields of a struct except that they cannot be used as field names in composite literals of the struct.
This is how you can solve it:
p := &Article{
Page: Page{"Welcome!"},
Id: 2,
}
Problem
I must be missing something. I cannot initialize an object's inherited fields without directly accessing them. My goal is trying to keep it simple. ``` package main type Page struct { Title string } type Article struct { Page Id int } func main() { // this generates a build error: // "invalid field name Title in struct initializer" // p := &Article{ Title: "Welcome!", Id: 2, } // this generates a build error: // "invalid field name Page.Title in struct initializer" // p := &Article{ Page.Title: "Welcome!", Id: 2, } // this works, but is verbose... trying to avoid this // p := &Article{ Id: 2, } p.Title = "Welcome!" // as well as this, since the above was just a shortcut // p := &Article{ Id: 2, } p.Page.Title = "Welcome!" } ``` Thanks in advance.