Create a function that accept any function in parameter

Hi, I need a function that can accept any function in parameter. My problem is, when I run my function " genFunc " with a function that has a parameter, it doesn’t work. I need to define my func type again ( anyFunc ) with parameter ( type anyFunc func(string) ). Is there a way to define a type function that accept any function?

package main

import (
	"fmt"
)

type anyFunc func()


func helloFunc() {
	fmt.Println("Hello world!")
}

func genFunc(gfunc anyFunc) {
	gfunc()
}

func main() {

	genFunc(helloFunc) // Print "Hello world!"
	
	genFunc(func(name string){
		// Not working...
	}) // cannot use func literal (type func(string)) as type anyFunc in argument to genFunc
}
1 Like

What do you mean by “again”? You have to define the type that genFunc is supposed to accept as a parameter. That’s what you do at

type anyFunc func()

and then at

func genFunc(gfunc anyFunc) {

What do you expect?

I mean, I need to define my function with parameter.
e.g: type anyFunc func(string) to accept a function with parameter.

func main()  {
    genFunc(helloFunc) // Print "Hello world!"

    genFunc(func(name string){
	    // Not working...
    }) // cannot use func literal (type func(string)) as type anyFunc in argument to genFunc

}

Yes, Go can not derive the type of the parameter by a func literal. You have to define a type.

Go’s type system is static and quite strict, and so func() is a different type than func(string). Besides that, even if you could pass a func(string) to genFunc, then genFunc would need a way to determine how many parameters that function expects, and pass those parameters to that function.

What do you want to achieve through this approach? I can imagine there are other ways of doing what you need to do.

1 Like

It’s like a generic function that can accept any type of function parameter.

genFunc(func()) // Works fine
genFunc(func(string)) // Not accepted

Yes, but why? What is the intent behind accepting any function signature? What is the use case?

1 Like

I want to execute my function serialize. execute is the same to genFunc.

Run().execute(func(){
	
	fmt.Println("func1: ","execute")
	
}).execute(func(){
	
	fmt.Println("func2: ","execute")
	
}).End()

What goal do you want to achieve with this construct that cannot be achieved otherwise?

If you want to build sophisticated language constructs, Go is perhaps not the right language for that. In a dynamically typed language, things like this are usually much easier to achieve.

If you want to solve a real-world problem, then there are surely other ways to do this.

2 Likes

Yeah your right, there are many ways to do this. I just created this construct to look like similar to GCD on iOS. I found out that method chaining through interface can achieve this. sounds crazy but I want to create my own package with this kind of construct. :slight_smile: but anyway thanks a lot @christophberger

1 Like

func genFunc(gfunc interface{})

You can find an examle here https://github.com/aws/aws-lambda-go/blob/master/lambda/entry.go

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.