Having written a lot of Go I sometimes find myself exasperated by functions like this:
because of all the repetition and vertical space. Working on something the other day I realized I could (ab-)use Go's switch like this:
because of all the repetition and vertical space. Working on something the other day I realized I could (ab-)use Go's switch like this:
PS You can play about with that here.
2 comments:
But of course you'd never use bare returns unless they make the function signature more clear, or as required for doing defer magic.... right? :)
Can't you just chain your new bool-returning functions with logical or? https://play.golang.org/p/iFsNFxD_ja
An 'on err: return 0, err' would bring the normal three lines down to a compact one. The expression would have to be bool or the built-in error type to avoid the noisy 'err != nil'.
Post a Comment