On Sunday, January 20, 2019 at 7:56:39 AM UTC-5, 伊藤和也 wrote: > > Yes I did. Can I say a method set is the one which associates an interface > with methods? > Almost. The method set *of an interface* is the set of methods in the interface. But other types, aside from interfaces, also have method sets. The method set of a type is the set of functions which can be called with an object of that type as a receiver. The last line of the spec, connects the two. It essentially says that if the method set of an object contains all the methods in an interface, then that object implements the interface. The object's method set may contain additional methods, but it must minimally contain all those in the interface if you want it to implement that interface. Perhaps an example:
https://play.golang.org/p/CpkTZ6jUJPU type A int func (a A) foo() { fmt.Println("A.foo") } func (a A) bar() { fmt.Println("A.bar") } func (a A) apple() { fmt.Println("A.baz") } type B int func (b B) foo() { fmt.Println("B.foo") } func (b *B) bar() { fmt.Println("B.bar") } func (b B) banana() { fmt.Println("B.baz") } type Thinger interface { foo() bar() } Following the spec <https://golang.org/ref/spec#Method_sets>at https://golang.org/ref/spec#Method_sets which says: > The method set of an interface type > <https://golang.org/ref/spec#Interface_types> is its interface. > So the method set of Thinger is foo() and bar(). The method set of any other type T consists of all methods > <https://golang.org/ref/spec#Method_declarations> declared with receiver > type T. > So the method set of type A is foo(), bar() and apple(), since they all take an A as the receiver. The method set of B is foo() and banana(), since they all take a B as the receiver. The method set of the corresponding pointer type > <https://golang.org/ref/spec#Pointer_types> *T is the set of all methods > declared with receiver *T or T (that is, it also contains the method set > of T). > So the method set of *B (a pointer to B) is foo(), bar() and banana(), since they all take a B or a *B as the receiver. The method set of a type determines the interfaces that the type implements > <https://golang.org/ref/spec#Interface_types> and the methods that can be > called <https://golang.org/ref/spec#Calls> using a receiver of that type. > So an object of type A implements the Thinger interface. An object of type B does *not *implement the Thinger interface, since it is missing bar(). But an object of type *B does implement the Thinger interface. See the playground version <https://play.golang.org/p/CpkTZ6jUJPU> for examples. This may not be 100% correct spec speak, hopefully will give you some sense of method sets is in Go. Hope this helps a little. > > 2019年1月20日日曜日 21時37分23秒 UTC+9 伊藤和也: >> >> What exactly is a method set? >> > -- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.