I've been trying to figure out why this is not working but am not sure. The error in the sandbox is
main.go:16: syntax error: unexpected {, expecting )
Code:
package main
import "fmt"
type handler func(a func(b int))
func HandleSomething(h handler) {
//...
//d := h(5)
//h(5)
// ...
}
func main() {
var foo int
HandleSomething(handler(func(func(b int){
fmt.Printf("debug: foo in main is %d and %d", foo, b)
})))
}
To see why it won't compile it might be helpful to pull your innermost function out into an argument as shown at https://play.golang.org/p/QPBturZ6GG
I think you are looking for something like the following, but it won't do much https://play.golang.org/p/2tEEwoZRC6
package main
import "fmt"
type handler func(a func(b int))
func HandleSomething(h handler) {
//...
//d := h(5)
//h(5)
// ...
}
func main() {
var foo int
HandleSomething(handler(func(a func(b int)) {
//This doesn't make sense as you don't have access to b
//fmt.Printf("debug: foo in main is %d and %d", foo, b)
fmt.Printf("debug: foo in main is %d", foo)
//You can call your function argument like
a(6)
a(7)
}))
}
The following example might give you something more to play with