2.3. Functions
Basics
The func keyword declares a function.
In the following example we declare the function add. It takes two parameters a and b of type int and returns a value of type int which is the sum of a and b.
| |
Output:
5
No Return Value
If a function does not return a result the return type can be left out.
| |
Output:
Hello Bob
Multiple Return Values
A function can return multiple values. The following example contains a function sub, which returns two values:
- The result of the subtraction of
aandb - A boolean which indicates if the result is negative
| |
Output:
-1 true
In Go it is an error to declare variables without using them. If we only need a single value we can discard variables by assigning them to _:
result, _ := sub(2, 3)
Returning Errors
Functions that can fail return error values. In many other languages exceptions are thrown to indicate error conditions. Go does not have exceptions.
Errors are returned from functions like every other return value. From the function signature we can see if a function can fail or not.
Let’s take a look at the function os.ReadFile from the Go standard library. It’s signature looks like this:
func ReadFile(name string) ([]byte, error)
In the function signature we see that the last return value is of type error. So we know that the function can fail. In the case of ReadFile possible errors could be that the file does not exist or that we do not have enough permission to read the file.
If the returned error value is not empty (nil) an error has occurred.
content, err := os.ReadFile("test.txt")
if err != nil {
// handle error
}
In many cases handling the error means:
- Passing the error up to the caller:
return err - Logging or printing an error (e.g. print to standard error with fmt.Fprintln )
| |
Output:
open test.txt: no such file or directory
Function As Values
In Go functions are first-class values. This means that you can pass functions around like ordinary values.
| |
Output:
5 -1