This question already has an answer here:
As we know that Go doesn't support optional parameters, but, while working with channels and more specifically buffered channels I come to the realisation that make
function do accept an optional parameter for buffer size. I'm little bit of confused if Go team doesn't like optional parameter then why they had supported a function which is so frequently used with this anti-pattern?
Well, to not deviate from the original question much, can anyone help explaining how, under the hood, this works?
</div>
make
is not a normal function. Neither is new
, len
, cap
, close
, append
, and a few more. Those are built-in functions.
Most built-in functions and operators are rewritten by the compiler to call normal functions in the runtime package.
For channels, the compiler rewrites the make
call to a call to runtime.makechan or one of the related variants.
Normal functions cannot have optional parameters.