Functions in GoLang have great ability to return multiple values. For a C programmer, this may sound as something weird. But with a quick experimentation, the ideas gets clear. How does GoLang orchestrate the multiple returning values is bit out-of-scope of this blog post, however, I’ll try to explain that aspect too in one of the blogs in the due course of time ahead.
Methods is GoLang should be understood as an extension to functions. A caller when invokes a function passes on certain things for the function to work on. We all know that “something” is a set of formal arguments. These formal arguments are treated as stack frames of the called function. They remain with the function and their scope is limited in the jurisdiction of the function. I believe, this isn’t new to those who know programming. Method in GoLang, as we’ve said earlier, is a kind of extension to function. This’s in essence that a method, apart from just the formal arguments that are optional, also have a receiver type associated with the same. The receiver can be a variable of any user defined type. Everything else remains the same as that with functions. So, if that’s the case, what’s the big deal with methods? What is so different with methods? Why do we need to learn an additional idiom? All these and many other such questions will be answered in the blog series on “GoLang interfaces” in times ahead. So, for now, just make it a point to learn methods in GoLang rather than arguing what is and what is not. And for the sake of argument, methods form the basis for interfaces.
Let’s get on. So, first thing first, the method has the following syntax: