Sobes.tech
Junior — Middle

Is it possible in the Go language to pass a function as an argument to another function?

sobes.tech AI

Answer from AI

Yes, in the Go language, functions are first-class citizens, so they can be passed as arguments to other functions, returned from functions, and assigned to variables.

Example of passing a function as an argument:

package main
import "fmt"

func applyOperation(a, b int, op func(int, int) int) int {
    return op(a, b)
}

func main() {
    sum := func(x, y int) int { return x + y }
    result := applyOperation(3, 4, sum)
    fmt.Println(result) // Outputs 7
}

Thus, Go supports a functional programming style with passing functions as parameters.

Is it possible in the Go language to pass a function… - sobes.tech