Definition of Method sets

145 views
Skip to first unread message

Yuu LongXue

unread,
Sep 4, 2020, 5:31:11 AM9/4/20
to golang-nuts
Hi all,

I’m confused by the "method set" defined in go specification,any one can help explain?

The Go Programming Language Specification says that :
```
The method set of any other type T consists of all methods declared with receiver type T. The method set of the corresponding pointer type *T is the set of all methods declared with receiver *T or T (that is, it also contains the method set of T).
```
but actually, the type T can use the method with receiver *T , which is described as bellow.


### version
1. Go: 1.14.2
2. The Go Programming Language Specification: Version of Jan 14, 2020

### code
```
type T struct {
ID int64
Name string
}

func (p T) GetNameA() string {
return p.Name
}

func (p *T) GetNameB() string {
return p.Name
}

func TestUser(t *testing.T) {
obj := T{ID: 1, Name: "T"}
na := obj.GetNameA()
nb := obj.GetNameB()
log.Printf("na=%v nb=%v", na, nb)
}
```

### output log
```
=== RUN TestT
2020/09/04 17:12:52 na=T nb=T
--- PASS: TestT (0.00s)
PASS
```

Thanks

YUU

Brian Candler

unread,
Sep 4, 2020, 5:50:22 AM9/4/20
to golang-nuts
On Friday, 4 September 2020 10:31:11 UTC+1, Yuu LongXue wrote:
Hi all,

        I’m confused by the "method set" defined in go specification,any one can help explain?

        The Go Programming Language Specification says that :
        ```
        The method set of any other type T consists of all methods declared with receiver type T. The method set of the corresponding pointer type *T is the set of all methods declared with receiver *T or T (that is, it also contains the method set of T).
        ```
        but actually, the type T can use the method with receiver *T , which is described as bellow.


It's because of the automatic referencing and dereferencing of pointers.

"If x is addressable and &x's method set contains mx.m() is shorthand for (&x).m():"

You are also using it in the opposite direction: inside GetNameB, p.Name is shorthand for (*p).Name.

Marvin Renich

unread,
Sep 4, 2020, 10:12:04 AM9/4/20
to golang-nuts
* Yuu LongXue <longx...@gmail.com> [200904 05:31]:
> Hi all,
>
> I’m confused by the "method set" defined in go
> specification,any one can help explain?
>
> The Go Programming Language Specification says that :
> ```
> The method set of any other type T consists of all methods
> declared with receiver type T. The method set of the
> corresponding pointer type *T is the set of all methods declared
> with receiver *T or T (that is, it also contains the method set
> of T).
> ```
> but actually, the type T can use the method with receiver *T ,
> which is described as bellow.

In addition to Brian's answer, the method set definition is specifically
about which types satisfy a particular interface. While the part of the
spec that Brian quoted describes a convenience shorthand, it does not
mean that a method declared with receiver *T is in the method set of
type T. This is a subtle point that can be confusing.

The playground link https://play.golang.org/p/N5IvMNvEsxi illustrates
this point. Note that you can call car.String() (line 38), but you
cannot assign car to a variable of type Stringer (line 43 produces a
compiler error). If you insert an '&' in front of car on line 43, it
compiles and runs.

This point is worth understanding.

...Marvin

Yuu LongXue

unread,
Sep 5, 2020, 12:27:24 AM9/5/20
to golang-nuts
yeah,this is really a  subtle point, Thank You For Your Addition.


On Sep 4, 2020, at 10:11 PM, Marvin Renich <mr...@renich.org> wrote:

Marvin

Reply all
Reply to author
Forward
0 new messages