Home > Back-end >  What's the difference between passing in a value or passing in an address into an interface in
What's the difference between passing in a value or passing in an address into an interface in

Time:10-08

Let's say we have a simple interface and implementation:

type Vertex struct {
    X int
    Y int
}

type Abser interface {
    Abs() float64
}

func (v Vertex) Abs() float64 {
    sum := float64(v.X   v.Y)
    return math.Sqrt(sum)
}

Now I have an interface variable:

var abser Abser

I want to set a vertex to it. I am able to set the value of one, or the address of one:

v := Vertex{1, 1}
abser = v
abser = &v

What is the difference between the two? Why does setting the address of a Vertex work? How does this tie in with how interfaces work under the hood? I'm still quite new to Go so any help would be much appreciated

  • Related