56

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?

softshipper
  • 32,463
  • 51
  • 192
  • 400

5 Answers5

50

It depends. There are several possibilities here.

  • If Renderer is passed around by value and the methods you need on Bitmap are defined on *Bitmap, then you'll need to embed *Bitmap.
  • If Renderer is passed around as a pointer, then you can embed Bitmap as a value without any problem (pointer methods will still be accessible in this case).
  • If Bitmap has a constructor function that returns a pointer, and the zero value of Bitmap isn't usable, you'll want to embed *Bitmap, as you don't want to encourage by-value copying of the Bitmap value.
  • If all the Bitmap methods are value methods, then you definitely want to embed by value.

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.

rog
  • 6,252
  • 4
  • 31
  • 25
15

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.

thwd
  • 23,956
  • 8
  • 74
  • 108
10

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.

ChiefQuimby
  • 109
  • 1
  • 3
8

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
jamlee
  • 1,234
  • 1
  • 13
  • 26
  • 1
    The main point of your code is if you define a value receiver setter function, that can not change the value passed in. Change or no change is few related with pointer or value embedding – jean Sep 25 '19 at 03:56
1

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)
Ben Weedon
  • 53
  • 4