Use closures instead

This is a better idiomatic Go approach that avoids having to manually define variables at the beginning.