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, 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.

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.

Generic function declarations

Function declarations … If the function declaration specifies type parameters, the function name denotes a generic function. A generic function must be instantiated before it can be called or used as a value. func min[T ~int|~float64](x, y T) T { if x < y { return x } return y } We’ve mentioned instantiation before, but it will be covered in depth later. For now, it’s enough to show an example of what the spec means can’t be done without instantiating a generic function:

Function declarations

No livestream today. My family is on vacation this week. I’ll be livestreaming again next week! Function declarations A function declaration binds an identifier, the function name, to a function. FunctionDecl = "func" FunctionName [ TypeParameters ] Signature [ FunctionBody ] . FunctionName = identifier . FunctionBody = Block . Nothing surprising here… So let’s move on to the first interesting rule: If the function’s signature declares result parameters, the function body’s statement list must end in a terminating statement.

No short declarations at package level

A couple last points about short variable declarations before we move on… Short variable declarations … Short variable declarations may appear only inside functions. In some contexts such as the initializers for “if”, “for”, or “switch” statements, they can be used to declare local temporary variables. So first: package foo id := 1 // Invalid. Short declarations only possible within fucntions var id = 1 // But this is valid. Don’t ask me why this rule exists.

Short variable declarations, cont'd

Today we’ll look at one of the most interesting, and I would say, confusing, aspects of short variable declarations. Short variable declarations … Unlike regular variable declarations, a short variable declaration may redeclare variables provided they were originally declared earlier in the same block (or the parameter lists if the block is the function body) with the same type, and at least one of the non-blank variables is new. As a consequence, redeclaration can only appear in a multi-variable short declaration.

Short variable declarations

Yesterday we saw how to define variables with var, but if you’ve ever actually used Go, you probably know that’s not actually the most common way to define variables. Let’s take a look at the alternative… Short variable declarations A short variable declaration uses the syntax: ShortVarDecl = IdentifierList ":=" ExpressionList . It is shorthand for a regular variable declaration with initializer expressions but no types: "var" IdentifierList "=" ExpressionList .

Unused variables

Variable declarations … Implementation restriction: A compiler may make it illegal to declare a variable inside a function body if the variable is never used. One short sentence. And people either love it, or hate it. I’m in the “love it” camp, but some people like the option to create unused variables, for later use, or during debugging. func foo() error { var i int // Some commented out code that probably used i // .

Declaring variable types

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, First some light refactoring, then adding a long-missing feature. Join me to see how many mistakes a senior Go dev makes while coding. We ended last week with a look at variable declarations. Today we continue that, with a look at how a variable’s type is determined.