Type parameters in method definitions

Yesterday we saw that when a method is defined on a generic type, the receiver must include type parameters. Now for all the relevant details: Method declarations … … Syntactically, this type parameter declaration looks like an instantiation of the receiver base type: the type arguments must be identifiers denoting the type parameters being declared, one for each type parameter of the receiver base type. The type parameter names do not need to match their corresponding parameter names in the receiver base type definition, and all non-blank parameter names must be unique in the receiver parameter section and the method signature.


Methods on generic types

Ready to dive back into generics, this time with regard to methods? Great, ‘cause here we go! Method declarations … If the receiver base type is a generic type, the receiver specification must declare corresponding type parameters for the method to use. Okay. Before we continue, let’s make sure we all know exactly what we’re talking about. What does it mean for a receiver base type to be a generic type?


Uniqueness of methods

Oops! Yesterday I live-streamed… to the wrong link! If you followed the link I shared yesterday, you were probably confused. But the good news is, you can still catch the replay. Method declarations … For a base type, the non-blank names of methods bound to it must be unique. If the base type is a struct type, the non-blank method and field names must be distinct. These uniqueness rules are quite predictible!


Receiver uniqueness

Today I’ll be love coding again. I hope you can join me! I’ll be continuing where I left off, working on the backlog for my open-source CouchDB SDK, https://kivik.io/. Join me to see how many mistakes a senior Go dev makes while coding. Method declarations … A non-blank receiver identifier must be unique in the method signature. If the receiver’s value is not referenced inside the body of the method, its identifier may be omitted in the declaration.

Subscribe to Boldly Go: Daily

Every day I'll send you advice to improve your understanding of Go. Don't miss out! I will respect your inbox, and honor my privacy policy.

Unsure? Browse the archive.


Method within selectors

Method declarations … The method is said to be bound to its receiver base type and the method name is visible only within selectors for type T or *T. This short sentence is a bit tricky to parse, because it relies on some as-yet unfamilar concepts. But it’s really pretty straight forward once those concepts are understood. A selector expression looks something like foo.bar. So given the following type and method:


Limitations on methods, part 3

This is part 3 of 3 of a mini-series on method limitations. If you missed the earlier installations, here are links to part 1 and part 2. Method declarations … A receiver base type cannot be a pointer or interface type and it must be defined in the same package as the method. And now for the conclusion: A receiver base type must be defined in the same package as the method.


Limitations on methods, part 2

Continuining from yesterday, when we started disecting this section of the spec: Method declarations … A receiver base type cannot be a pointer or interface type and it must be defined in the same package as the method. Today we’re tackling the second limitation: A receiver base type cannot be an interface. At first this might seem a bit counter-intuitive… aren’t interfaces all about methods? Well, yes, but in the other directions.


Limitations on methods

Today I’ll be love coding again. I hope you can join me! I’ll be working on the backlog for my open-source CouchDB SDK, https://kivik.io/ to add a long-missing feature. Join me to see how many mistakes a senior Go dev makes while coding. Method declarations … A receiver base type cannot be a pointer or interface type and it must be defined in the same package as the method. This tiny sentence has three distinct cases to consider.


Method receiver base types

There’s a lot to unpack about method receiver types, so we’ll do it over two days. Method declarations … The receiver is specified via an extra parameter section preceding the method name. That parameter section must declare a single non-variadic parameter, the receiver. This bit is fairly straight forward. The receiver is specified in an extra parameter section preceeding the method name: func (reciever ReceiverType) MethodName(/* 0 or more regular arguments */) { /* .


Method declarations

Now that we’ve covered function declarations, let’s discuss the more nuanced topic of method declarations, which can be thought of as special case of of function declarations. Method declarations A method is a function with a receiver. A method declaration binds an identifier, the method name, to a method, and associates the method with the receiver’s base type. MethodDecl = "func" Receiver MethodName Signature [ FunctionBody ] . Receiver = Parameters .


Empty function declarations

Function declarations … A function declaration without type parameters may omit the body. Such a declaration provides the signature for a function implemented outside Go, such as an assembly routine. func flushICache(begin, end uintptr) // implemented externally This is something I have never used. Most developers probably never will. Although I’ve certainly seen it a few times, particularly while browsing the standard library source code, so it’s good to be aware of it, so you aren’t stuck scratching your head when you see it.