Go by Example - Functions
Go by Example - Functions
com/functions
Go by Example: Functions
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 ints and returns func plus(a int, b int) int {
their sum as an int.
When you have multiple consecutive parameters func plusPlus(a, b, c int) int {
of the same type, you may omit the type name for return a + b + c
the like-typed parameters up to the final }
parameter that declares the type.
func main() {
res = plusPlus(1, 2, 3)
fmt.Println("1+2+3 =", res)
}
$ go run functions.go
1+2 = 3
1+2+3 = 6
1 of 1 11/26/24, 23:27