Can functions be passed as parameters GO? [SOLVED]


Written By - Tuan Nguyen
Advertisement

In this tutorial, we will learn how to pass functions as a parameter to another function in Golang. Since Golang functions are first-order variables so it has some features:

  • They can be assigned to a variable
  • Passing to another function as parameters
  • Return from a function

The function is a type in GO as well. If two functions share the same arguments and return values, they are said to be of the same type. The precise function signature must be supplied in the argument list when giving a function as an argument to another function.

 

Syntax for using golang functions

We have already explained golang function in detail but to cover this tutorial, here is the syntax:

func print(f func(int) int, val int)

Explanation:

The print function has two parameters:

  • the f function which takes an int as the parameter and returns an int
  • an integer val

 

Example 1: Pass a function to another one as a parameter

In the code shown below, we will try to write a function that takes another function as the parameter:

package main

import "fmt"

func cube(i int) int {
	fmt.Println("This is the cube function")
	return i * i * i
}
func square(i int) int {
	fmt.Println("This is the square function")
	return i * i
}
func print(f func(int) int, val int) {
	fmt.Println("The initial value is: ", val)
	fmt.Println(f(val))
}
func main() {
	print(cube, 11)
	print(square, 11)
}

Output:

The initial value is:  11
This is the cube function
1331
The initial value is:  11
This is the square function
121

Explanation:

Advertisement
  • In the example below, we have 2 function cube and square which takes an int as a parameter and return an int
  • We have the print function which takes a function and an int as parameters and returns nothing. In this function, we will call and print out the return value of the parameter function.

 

Example 2: Define a struct and pass it as a parameter to a function

The idea is the same as the first example except that instead of directly passing the function, we will create a struct and pass this struct as a parameter to the function:

package main

import "fmt"

// define a struct
type fn func(int) int

func cube(i int) int {
	fmt.Println("This is the cube function")
	return i * i * i
}
func square(i int) int {
	fmt.Println("This is the square function")
	return i * i
}

// pass the struct to a function
func print(f fn, val int) {
	fmt.Println("The initial value is: ", val)
	fmt.Println(f(val))
}

func main() {
	print(cube, 11)
	print(square, 11)
}

Output:

The initial value is:  11
This is the cube function
1331
The initial value is:  11
This is the square function
121

 

Example 3: Passing an anonymous function as an argument into another function

You can also pass an anonymous function as an argument into another function. The code shown below demonstrates how we can do that in Golang:

package main

import "fmt"

// function as parameter
func GoLinuxCloud(f func(p, q string) string) {
	fmt.Println(f("GoLinux ", "Cloud"))
}

func main() {
	// pass anonymous function
	GoLinuxCloud(func(p, q string) string {
		return p + q + " Have a nice day!"
	})
}

Output:

GoLinux Cloud Have a nice day!

 

Summary

We always want to pass functions to another function as parameters, when it comes to applying any functionality with optional functionality which we need to add up to the parent functionality. Hope with the explained code above you can understand how to do that in Golang.

 

References

https://go.dev/tour/basics/11
go - Can functions be passed as parameters? - Stack Overflow

 

Categories GO

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment