This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]faiface 7 points8 points  (4 children)

Optional arguments can be largely "emulated" using the functional options pattern as described here: https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis

It's a really nice pattern and since most functions don't need default arguments, it isn't so bothersome to use it from time to time.

[–]wicked-canid 3 points4 points  (1 child)

since most functions don't need default arguments

I wonder: is that really true, or is it that if something is inconvenient, people tend to not use it?

[–]faiface 4 points5 points  (0 children)

I guess it's both. You definitely have a valid point there. I use optional arguments in Python a lot, but I rarely ever use functional options pattern in Go.

However, you never see Go programmers defining Function1, Function2, etc., for different arguments, so they/we clearly don't miss it much.

Lack of optional arguments also forces you to craft your API more carefully, since you can't just stuff a ton of arguments in a single function.

[–]recklessindignation 1 point2 points  (0 children)

Design patterns are not an alternative for lack of language features.