Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding in Go with pointer or with value

Tags:

go

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?

like image 734
softshipper Avatar asked Feb 13 '15 14:02

softshipper


4 Answers

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.

like image 136
rog Avatar answered Nov 13 '22 01:11

rog


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.

like image 35
thwd Avatar answered Nov 13 '22 02:11

thwd


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.

like image 8
ChiefQuimby Avatar answered Nov 13 '22 02:11

ChiefQuimby


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
like image 7
jamlee Avatar answered Nov 13 '22 02:11

jamlee