what is embedded struct in Go
tags: learning go programming
content
- i see this from static analysis on this line of code:
m.renderer.ViewRange.LastRowIndex
could remove embedded field "ViewRange" from selector
- this is
ViewRangeis an embedded struct - it’s defined by something like this:
type ViewRange struct {
// some fields here
LastRowIndex int
}
type Renderer struct {
// some fields here
ViewRange
}- NOTE:
ViewRangeinRendererdoes NOT have a field name - this is an embedded struct
- all of embedded struct’s fields is promoted to its parent
- so, in order to access
LastRowIndex- instead of
m.renderer.ViewRange.LastRowIndex- we can have:
m.renderer.LastRowIndex