Go Functions
Links: 103 Golang Index
Functions¶
- Go recommends writing function names in simple word or camelCase.
Within the same package function names must be unique!
- One of Go's features is that functions and methods can return multiple values.
- Go doesn't support function overloading.
- In Go functions are first class citizens. This means
- Functions can be assigned to variables
- Can be passed as arguments to other functions
- Or return from other functions
Defining functions¶
- Shorthand parameter notationThere are no default arguments in Go.
Returning values¶
- If the function is returning something its return type must be specified
- Simple return
Multiple return¶
- If
error
is returned then as a convention it should be the last parameter - For multiple return values type must be inside parenthesis
Named Return¶
- We can return the named parameters without any value.
- This is known as naked return and should only be used in short functions as it harms the readability in long functions.
- We can also return multiple values with named return
Variadic Functions¶
- Variadic functions take variable number of arguments
- We use variadic function when the number of arguments is unknown
- Ellipsis prefix (three-dots) in front of the parameter type makes a function variadic.
- The function may be called with 0 or more parameters
- If the function takes parameters of different types then only the last parameter of the function can be variadic.
fmt.Println
is an example of variadic function- We can paste a slice to a variadic function by postfixing it with the variadic operator (
...
)- You can think it as taking out all the values of slice and then passing them to the function
- We cannot pass arrays to variadic functions
- variadic functions use slices and
[]int != [4]int
- variadic functions use slices and
- Example
Defer¶
- A defer statement postpones the execution of a function until the surrounding function returns but before any return value.
- If there are multiple deferrals then go will execute them in the reverse order of their deferral. Think of it as going from bottom to top (only for the deferred functions).
- The arguments of the deferred functions are evaluated immediately but not executed until the surrounding function returns.
-
defer
statement is used to make sure some function is executed later for cleanup like when opening a file we defer the file close function.func f1() { fmt.Println("This is function 1") } func f2() { fmt.Println("This is function 2") } func f3() { fmt.Println("This is function 3") } func main() { defer f1() // 5 f2() // 1 fmt.Println("penultimate statement") // 2 defer f3() // 4 fmt.Printn("last statement") // 3 } // This is function 2 // penultimate statement // last statement // This is function 3 // This is function 1
-
Example: Arguments evaluated at time defer is executed, not at time of function execution.
Anonymous Function¶
- These functions don't have any name and are declared inline.
- These can be used to form closures.
// saying that we are returning a function of type int func increment(a int) func() int { // returning an anonymous function return func() int { a++ return a } // we are not executing the function immediately as in above example } func main() { inc := increment(10) fmt.Printf("%T\n", inc) // func() int fmt.Printf("%#v\n", inc) // (func() int)(0x47f6c0) inc() fmt.Println(inc()) // 12 }
- Passing functions to variables
- With function signatures and declaration
Miscellaneous¶
- A feature that go has which is very rare to have is returning a local variable as a pointer.
- Explanation
- result variable is declared in the execution stack of the sum function
- General expectation is that when we come out of sum function its execution stack will be freed up and in other languages this is not a safe operation. You are returning a pointer to a location which just got free.
- On the other hand in go when it sees that you are returning a local variable as a pointer it is automatically going to promote it to the shared program memory (heap memory)
Last updated: 2022-06-12