The idea behind the Method Sets


(Peter Bocan) #1

Hello guys, I have quite a deep C/C++ knowledge and been learning Go for 3 days now and I’ve come across the Method sets. What’s the driving idea behind method sets? Why dividing methods into two subsets of two receiver types? I kinda can’t get my head around it and just logically justify it.


(Jay Ts) #2

Hi Peter,

Well, it may take you more than three days or a week to understand this because it’s one of the few parts of Go that isn’t really simple, and is very different from C-like languages.

I think you need to learn about this by studying interface types because that’s what method sets are for. Here it is in the Go Programming Language Specification, but you will probably find some other resource is better at explaining it.

https://golang.org/ref/spec#Method_sets

If you understand why Go has interfaces and exactly what they are and how they work, it follows directly that method sets are needed for implementing interfaces.

It works quite differently than in C++. Go does not have classes. Methods are associated with types. If you have the required set of methods for a type, then the type implements an interface that has those methods as its method set.