Functions are central in Go. We’ll learn about functions with a few different examples. |
|
package main
|
|
import "fmt"
|
|
Here’s a function that takes two |
func plus(a int, b int) int {
|
Go requires explicit returns, i.e. it won’t automatically return the value of the last expression. |
return a + b
}
|
func main() {
|
|
Call a function just as you’d expect, with
|
res := plus(1, 2)
fmt.Println("1+2 =", res)
}
|
$ go run functions.go
1+2 = 3
|
|
There are several other features to Go functions. One is multiple return values, which we’ll look at next. |
Next example: Multiple Return Values.