One of the things I love about Go is how they encourage passing errors as return values, but when it comes to logging what is the most maintainable solution: passing the error as far back down the execution stack as possible and logging everything there, or logging it in the function it occurred there and then?
So the former:
func main() {
err := DoSomething()
if err != nil {
log.Printf(err)
}
}
func DoSomething() error {
return DoSomethingElse()
}
func DoSomethingElse() error {
return ThisFunctionMayCauseAnErrorAndIsHorriblyNamed()
}
Or
func main() {
_ := DoSomething()
}
func DoSomething() error {
return DoSomethingElse()
}
func DoSomethingElse() error {
err := ThisFunctionMayCauseAnErrorAndIsHorriblyNamed()
if err != nil {
log.Printf(err)
}
}