FAQ
I'm running through the Go tour:

http://tour.golang.org/#52

On that example, if you press Run it outputs:

prog.go:19: cannot use v (type Vertex) as type Abser in assignment:
Vertex does not implement Abser (Abs method requires pointer receiver)


However, if you change "func (v *Vertex) Abs()" to "func (v Vertex) Abs()" I was expecting it to throw an error on:

a = &v

The inverse of the other error. But it works fine. Can someone please explain to me why this is?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • David Symonds at Apr 16, 2013 at 6:49 am
    The first point to get a strong grip on is that T and *T are different
    types. Vertex and *Vertex are different. There's certain mechanism for
    how their method sets overlap, but this basic principle is why you get
    that error: Abs is a method on *Vertex not Vertex, so *Vertex
    satisfies the interface, not Vertex.

    Now, that overlap I mentioned explains why "a = &v" works. It's
    documented at http://golang.org/ref/spec#Method_sets, but the basic
    idea is that *T gets the methods on T for free, because it's a trivial
    operation to dereference a pointer at invocation time. That means that
    when Abs is a method on Vertex, it is also treated as a method on
    *Vertex.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Péter Szilágyi at Apr 16, 2013 at 6:59 am
    To add a small detail to David's explanation, the reason why you can
    (always) convert a pointer to a value but not the inverse, is because if a
    value is a literal, it does not reside in addressable memory (or even
    memory depending on the case).

    Consider (forced types for clarity):

    a := int(10) // Ok, value
    b := &a // Ok, pointer
    c := *b // Ok, pointer dereference
    d := &int(10) // Hmm... oops

    Cheers,
    Peter

    On Tue, Apr 16, 2013 at 8:49 AM, David Symonds wrote:

    The first point to get a strong grip on is that T and *T are different
    types. Vertex and *Vertex are different. There's certain mechanism for
    how their method sets overlap, but this basic principle is why you get
    that error: Abs is a method on *Vertex not Vertex, so *Vertex
    satisfies the interface, not Vertex.

    Now, that overlap I mentioned explains why "a = &v" works. It's
    documented at http://golang.org/ref/spec#Method_sets, but the basic
    idea is that *T gets the methods on T for free, because it's a trivial
    operation to dereference a pointer at invocation time. That means that
    when Abs is a method on Vertex, it is also treated as a method on
    *Vertex.

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedApr 16, '13 at 6:43a
activeApr 16, '13 at 6:59a
posts3
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase