Go function simple example

In this example, we will show you how to create a simple function in Go with an example.

A function is a mapping of zero or more input parameters to zero or more output parameters.

Go function simple example

Functions in Go are created with the func keyword. We use the return keyword to return values from functions.

The following example creates a simple function in Go:


package main

import "fmt"

func main() {

    x := 4
    y := 5

    z := add(x, y)

    fmt.Printf("Output: %d\n", z)
}

func add(a int, b int) int {

    return a + b
}

Output:

Output: 9

In the above example, we define a function that adds two values.