Golang functions & methods simplified


If you are exploring Golang (aka Go) you must have faced some difficulty in understanding the method concepts, especially if you are coming from a Java background which is more statically typed. It gets more complicated when you get into methods with pointers.

go_lang1

Tour of Go gives step by step guide with example programs and an environment to edit & execute them on the fly. But some of them are not so good as examples, coz they eat up your time in understanding the programming logic first before understanding the Go concepts they were intended to explain. In this post I have tried to explain the Go functions and methods with pointers in a simple manner.

A function in Go follows the syntax:

func FunctionName(Parameters...) ReturnTypes...

Example:

func add(x int, y int) int

To execute:

add(2,3)

A method is like a function, but attached to a type (called as receiver). The official guide states A method is a function with a special receiver argument”. The receiver appears in between the func keyword and the method name. The syntax of a method is:

func (t ReceiverType) FunctionName(Parameters...) ReturnTypes...

Example:

func (t MyType) add(int x, int y) int

To execute:

type MyType string
t1 := MyType("sample")
t1.add(1,2)

Now lets bring pointers into the table. Go lang is pass by value, means fresh copies of the parameters are passed to each function/method call. To pass them by reference you can use pointers.

Syntax of function with pointer in argument/parameter list. 

func FunctionName(*Pointers...,Parameters...) ReturnTypes...

Example

func add(t *MyType, x int, y int) int

To execute:

type MyType string
t1 := MyType("sample")
add(&t1,4,5)

Similarly for methods, the receiver type can be a pointer. Syntax of method with pointer (as receiver)

func (*Pointer) FunctionName(Parameters...) ReturnTypes...

Example

func (t *MyType) add(x int, y int) int

To execute:

type MyType string
t1 := MyType("sample")
t1.add(2,3)

Note that we can still write t1.add() to execute the method with a pointer receiver(even-though ‘t1’ is not a pointer) and Go will interpret it as (&t1).add(). Similarly a method with value receiver can be called using pointer too, Go will interpret p.add() as as (*p).add() in that case (where ‘p’ is a pointer). This is applicable only for methods and not for functions.

Methods with pointer receiver are very useful to get a “Java” like behavior where the method is actually modifying on the value to which the receiver points and not on a copy of it.

Hope this post gave you good understanding of the functions & methods concepts, please let me know your comments. I have also forked the golang/tour branch and updating the example programs with simple ones. You can download/contribute to it at github.com/anilgkurian/tour.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s