When I want to embed a struct within another struct, should I use a pointer or value?
For example
type Job struct {
Command string
*log.Logger
}
or
type Job struct {
Command string
log.Logger
}
When I want to embed a struct within another struct, should I use a pointer or value?
For example
type Job struct {
Command string
*log.Logger
}
or
type Job struct {
Command string
log.Logger
}
You can use one or the other: for struct type, the spec mentions:
Since
log.Logger
is not an interface, you can use the type or a pointer to the type for the anonymous fieldLogger
.The article "Embedding in Go " fro Eric Urban (
hydrogen18
) calls embedding a pointer "embed by-pointer":For instance, with:
The
Renderer
type embeds aBitmap
by-pointer.As mentioned in this thread: