Go supports recursive functions like other Algol-family languages. Each call gets its own stack frame; deep recursion can overflow the stack — prefer iteration or a trampoline for very deep problems.
func fact(n int) int {
if n <= 1 {
return 1
}
return n * fact(n-1)
}
type Node struct {
Value int
Left *Node
Right *Node
}
func (n *Node) Sum() int {
if n == nil {
return 0
}
return n.Value + n.Left.Sum() + n.Right.Sum()
}
func even(n int) bool {
if n == 0 {
return true
}
return odd(n - 1)
}
func odd(n int) bool {
if n == 0 {
return false
}
return even(n - 1)
}