Recursive inner function declaration golang
Is it supposed to be ugly?
I'm currently trying to write a recurisve DFS for a leetcode problem (new to Golang)
Doesn't Run:
when I try to create and declare my inner function like this:
outerFunction (node *TreeNode, target int) bool {
checkSolutions := func(node *TreeNode, total int) bool {
// ... DFS algo
checkSolutions(node.Left)
checkSolutions(node.Right)
}
return checkSolution(root, 0)
}
I don't have access to the inner function during the recursive call and get an error! However when I declare the function as a variable first (below) it runs
Runs:
outerFunction (node *TreeNode, target int) bool {
var checkSolution func(*TreeNode, int) bool
checkSolutions = func(node *TreeNode, total int) bool {
// ... DFS algo
checkSolutions(node.Left)
checkSolutions(node.Right)
}
return checkSolution(root, 0)
}
Is this the cleanest way to declare recursive inner functions in Go? For some reason it feels a little bit verbose to me so I just wanted to make this post to see if there's any Golang syntactic sugar intended for this situation that I'm missing.