CodeToLive

Functions in Go

Functions in Go are reusable blocks of code that perform a specific task. They can take parameters and return values. Functions are a fundamental building block of Go programs, enabling modularity and code reuse.

Defining a Function

Functions in Go are defined using the func keyword. The function name is followed by a parameter list (if any) and a return type (if any).


package main

import "fmt"

func greet(name string) {
    fmt.Println("Hello,", name)
}

func main() {
    greet("Alice")
}
      

Returning Values

Functions can return one or more values. The return type is specified after the parameter list. If a function returns multiple values, they are enclosed in parentheses.


package main

import "fmt"

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

func main() {
    result := add(5, 10)
    fmt.Println("Sum:", result)
}
      

Multiple Return Values

Go supports returning multiple values from a function. This is commonly used for functions that need to return both a result and an error.


package main

import "fmt"

func swap(a, b int) (int, int) {
    return b, a
}

func main() {
    x, y := swap(10, 20)
    fmt.Println("x:", x, "y:", y)
}
      

Named Return Values

Go allows you to name the return values of a function. These named values are treated as variables within the function, and you can return them implicitly.


package main

import "fmt"

func divide(a, b float64) (result float64, err error) {
    if b == 0 {
        err = fmt.Errorf("division by zero")
        return
    }
    result = a / b
    return
}

func main() {
    result, err := divide(10, 0)
    if err != nil {
        fmt.Println("Error:", err)
        return
    }
    fmt.Println("Result:", result)
}
      

Variadic Functions

Variadic functions can accept a variable number of arguments. The arguments are treated as a slice inside the function.


package main

import "fmt"

func sum(numbers ...int) int {
    total := 0
    for _, num := range numbers {
        total += num
    }
    return total
}

func main() {
    result := sum(1, 2, 3, 4, 5)
    fmt.Println("Sum:", result)
}
      

Anonymous Functions

Anonymous functions are functions without a name. They are often used for short-lived tasks or as arguments to other functions.


package main

import "fmt"

func main() {
    func() {
        fmt.Println("This is an anonymous function!")
    }()

    // Assigning an anonymous function to a variable
    greet := func(name string) {
        fmt.Println("Hello,", name)
    }
    greet("Alice")
}
      

Closures

A closure is a function that captures and retains references to variables from its surrounding lexical scope. Closures are useful for creating functions with state.


package main

import "fmt"

func counter() func() int {
    count := 0
    return func() int {
        count++
        return count
    }
}

func main() {
    increment := counter()
    fmt.Println(increment()) // Output: 1
    fmt.Println(increment()) // Output: 2
    fmt.Println(increment()) // Output: 3
}
      

Best Practices

Next: Structs