Functions

Go functions are similar to functions in other programming languages.

  • A function can take zero or more arguments.
  • Arguments can have default values.
  • The last argument can be variadic (variable lenght).









Some things to be aware of

  • Functions can return more than one value.
  • Functions can return labeled variables.
  • Functions always receive arguments by value.









Syntax and use:

func load() {}
func load(file string) {}

func add(a int, b int) int {
    return a + b
}

func subtract(a, b int) int {
    return a - b
}

func addAndSubtract(a int, b int) (int, int) {
    return a + b, a - b
}

Labelled return values, but try to limit the use of this as it obscures where return values are set as functions grow tall.

func multiply(a, b int) (result int) {
    result = a * b
    return
}









Using function return values

Escape the return values you don't need with _.

func main() {
    sum, _ := addAndSubtract(1, 2)
    fmt.Println(sum)
}


Why would you want to escape a return value?