Functions
- Similar to other languages
- It can receive arguments
- Arguments can have default values
- The last argument can be of variable length
The tricky part
- Functions can return more than one value (at once)
- Functions can return labeled variables
- Functions receive arguments always by value
func add(a int, b int) int {
return a+b;
}
func addAndSubstract(a int,b int)(int,int){
return a+b,a-b
}
addition, substraction := addAndSubstract(4,2)
_, substraction := addAndSubstract(4,2)
addition, _ := addAndSubstract(4,2)
Functions receiving references
One package one main()
func birthday(age *int){
age = *age + 1
}
func main(){
age := 22
birthday(&age)
fmt.Prinln(age)
}
Functin Curiosity
panic("Message!")
// It will close your appdefer fmt.Println("bye")
// It will delay execution to the end. It uses stack data structureinit()
//
Errors Design Pattern
We don’t have exceptions in GO, this is the typical design pattern when an action may trigger an error.