Functions
Functions are fundamentals of Go. A function in Go can be declared with func (v T) T{}
where v is value and T is type, while func
is declaration. Function in Go require return
explicitly, it will not automatically return last statement as value. Here is very basic function in Go
Yes, you see that right. We have been writing function since our first program. This is main function that always has to be part of package main
and it is the entry point of program in Go
Though this function is very basic, it does not accept any parameter and not returning any value in this case. Lets write a trivial program that sums two integers
The function sum
takes two argument of type int
and returns int
. In Go, one can define multiple consecutive parameters of same type as once. For example, above example can be rewritten as
Here we can see that since a and b both are of same type and are consecutive parameters, we can define the type at once. Function in Go are called with there name followed by parameters in parenthesis. e.g sum(1, 2)
Multiple return values
Function in Go can return, multiple return values of any types. For illustration look at the following example
This will automatically assign return values to a and b. There are sometimes where function returns multiple values but we only need one or two, but assigning those to variables and not using, will make Go complain about unused variable. To overcome this scenario return value can be assigned to _
. Hypothetically in example above, if we are not going to use area but only perimeter, then we can do something like this
This would make Go not to complain unused variables.
Variadic Function with Splat argument
Sometimes in some scenarios we need a function that would work with multiple number of arguments. In some programming languages this is achieved by method overloading, where you can have method with same name but varying signature. Go implements this in a simpler form called variadic function with splat argument
Do not get surprised but we are using a variadic function since our first program, fmt.Println
. This function takes a varying number of argument of same type. for example below calls to fmt.Println
are valid
Go allows programmer to write a variadic function with splat arguments. Let's re-write our sum
function
You get the idea. Do not panic regarding range
, we will soon be covering that
Last updated