I can embed in golang with pointer and value. By pointer
type Bitmap struct{
data [4][4]bool
}
type Renderer struct{
*Bitmap
on uint8
off uint8
}
By value
type Bitmap struct{
data [4][4]bool
}
type Renderer struct{
Bitmap
on uint8
off uint8
}
What is more prefer by pointer or value?
It depends. There are several possibilities here.
In the specific case you have here, I'd probably embed by value, as the type is small - it gives you locality of access and less memory allocations.
By embedding a type you usually want to benefit from call-forwarding. *Bitmap
's method set is a super set of Bitmap
's method set. So in most cases you'll want to embed *Bitmap
, unless all its methods have a receiver of type Bitmap
or the method set it empty, in which cases you can avoid the indirection.
I've also found it useful when you have multiple structs all with the same embedded base type, and you want to use a helper function which modifies the values of the base struct. For example, given the following structs:
type Base struct {
F1 int
}
type A struct {
*Base
Fa int
}
type B struct {
*Base
Fb int
}
and the following helper function:
func modstruct(base *Base) {
base.F1 = 2
}
all of the following function calls will compile, and modify values of the struct:
base := &Base{}
a := &A{Base: &Base{}}
b := &B{Base: &Base{}}
modstruct(base)
modstruct(a.Base)
modstruct(b.Base)
There seems so be a misunderstanding of receivers, as expressed in rog's answer. Methods (receivers) are not "defined on" a pointer or a type, the same methods can be called on the value-of-type as the pointer, the receiver's signature only determines whether it receives value-of-type or a pointer to value-of-type. That is to say func(t *YourType)
can be called on YourType
or &YourType
and vice-versa with a value receiver. I think this should clarify: https://play.golang.org/p/AT1J2oGkum So as to the question as to whether to embed a value or pointer...the referentiality is really determined by how you deal with the outer object, if you are passing a pointer to the outer struct you will have access to the same embedded struct value, if you are passing the value of the outer struct, do you want it to point to the "original" underlying value of the embedded struct or a copy? I think in most cases you will either want to embed a pointer and pass pointers to your outer struct, or embed a value and pass value of your outer struct.
i tried: https://play.golang.org/p/IVM5OoDU9ZN
package main
import (
"fmt"
)
type Base struct {
Name string
}
func (b Base) PrintName() {
fmt.Println(b.Name)
}
func (b *Base) PrintNameP() {
fmt.Println(b.Name)
}
func (b Base) ChangeName(name string) {
b.Name = name
}
func (b *Base) ChangeNameP(name string) {
b.Name = name
}
type EmbedsBase struct {
Base
}
type EmbedsPointerToBase struct {
*Base
}
func main() {
fmt.Println("")
fmt.Println("# embed by value and refrenced by value, not change origianl value")
b := Base{"Jeff Hardy"}
eb := EmbedsBase{b}
eb.PrintName()
eb.ChangeName("John Cena")
eb.PrintName()
fmt.Println("")
fmt.Println("# embed by value, but refrenced by pointer, changed origianl value")
b = Base{"Jeff Hardy"}
ebp := &EmbedsBase{b}
ebp.PrintNameP()
ebp.ChangeNameP("John Cena")
ebp.PrintNameP()
fmt.Println("")
fmt.Println("# embed by pointer, but refrenced by value, not chage origianl value")
b = Base{"Jeff Hardy"}
epb := EmbedsPointerToBase{&b}
epb.PrintName()
epb.ChangeName("John Cena")
epb.PrintName()
fmt.Println("")
fmt.Println("# embed by pointer, and refrenced by pointer, changed origianl value")
b = Base{"Jeff Hardy"}
epbp := &EmbedsPointerToBase{&b}
epbp.PrintNameP()
epbp.ChangeNameP("John Cena")
epbp.PrintNameP()
}
the result of above is:
# embed by value and refrenced by value, not change origianl value
Jeff Hardy
Jeff Hardy
# embed by value, but refrenced by pointer, changed origianl value
Jeff Hardy
John Cena
# embed by pointer, but refrenced by value, not chage origianl value
Jeff Hardy
Jeff Hardy
# embed by pointer, and refrenced by pointer, changed origianl value
Jeff Hardy
John Cena