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
func main(){
fmt.Println("This is Awesome")
}
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
package main
import "fmt"
func sum(a int, b int) int {
return a + b
}
func main(){
fmt.Print("Adding 1 and 2 equals: ")
fmt.Println(sum(1, 2))
}
// Adding 1 and 2 equals: 3
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
package main
import "fmt"
func sum(a, b int) int {
return a + b
}
func main(){
fmt.Print("Adding 1 and 2 equals: ")
fmt.Println(sum(1, 2))
}
// Adding 1 and 2 equals: 3
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
func AreaAndPerimeterSqr(a int) (int, int) {
return a*a, 4*a
}
// Same can be called with
a, b := AreaAndPerimeterSqr(2)
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
// To ignore value or related warnings for unused vars use _
_, b := AreaAndPerimeterSqr(2)
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
fmt.Println("Hello")
fmt.Println("Hello Go")
fmt.Println("Hello Go World")
Go allows programmer to write a variadic function with splat arguments. Let's re-write our sum
function
package main
import "fmt"
func sum(nums ...int) int {
total := 0
for _, num := range nums {
total += num
}
return total
}
func main(){
fmt.Println("1 + 2: ", sum(1, 2))
fmt.Println("1 + 2 + 3: ", sum(1, 2, 3))
fmt.Println("1 + 2 + 3 + 4: ", sum(1, 2, 3, 4))
fmt.Println("1 + 2 + 3 + 4 + 5: ", sum(1, 2, 3, 4, 5))
fmt.Println("1 + 2 + 3 + 4 + 5 + 6: ", sum(1, 2, 3, 4, 5, 6))
}
// 1 + 2: 3
// 1 + 2 + 3: 6
// 1 + 2 + 3 + 4: 10
// 1 + 2 + 3 + 4 + 5: 15
// 1 + 2 + 3 + 4 + 5 + 6: 21
You get the idea. Do not panic regarding range
, we will soon be covering that
Last updated
Was this helpful?