softshipper
softshipper

Reputation: 34099

Embedding in Go with pointer or with value

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?

Upvotes: 61

Views: 21774

Answers (6)

Victor
Victor

Reputation: 3978

Is worth mentioning that embedding with pointer has the nil pointer perils... and due to the syntactic sugar that type embedding allows you may not see the nil issue.

Allow me to show my words with code:

package main

import (
    "fmt"
)

type Simple struct {
    x int
}

func (s Simple) PrintMe() {
    fmt.Printf("%+v\n", s)
}

type Composed struct {
    *Simple
}

func main() {
    var c Composed = Composed{
        Simple: nil,
    }
    // the below statements will produce a panic by nil pointer
    c.PrintMe()               // the syntactic sugar that type embedding allows
    c.Simple.PrintMe()        // this is another syntax to call PrintMe method
    Simple.PrintMe(*c.Simple) // as far I know a method is associated with a Type (not with an instance)... and "the end of the day" this is what actually golang does (for us) when we wrote  "c.PrintMe()". And I think this way (with this statement) we can see more clearly when the nil pointer panic arises as the expression `c.Simple` yields nil thus the attempt to eval the resultant expression `*nil` produces the panic
}

In Playground

I Also found this answer as one of the most simple and enlighten for those that want to start investigating, although it is not about embedding it turned out to be a good starting point (at least for me) to dive deep into the study of nested structures and issues like this.

Upvotes: 0

jamlee
jamlee

Reputation: 1353

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

Upvotes: 10

ChiefQuimby
ChiefQuimby

Reputation: 119

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.

Upvotes: 11

Ben Weedon
Ben Weedon

Reputation: 63

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)

Upvotes: 2

rog
rog

Reputation: 6690

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.

Upvotes: 56

thwd
thwd

Reputation: 24858

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.

Upvotes: 16

Related Questions