0

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?

Aziz
  • 928
  • 1
  • 10
  • 18
  • 3
    The point regarding `make` is an interesting one, it seems `make` is a special language construct (rather than a normal function) so the rules don't apply the same to it. See https://stackoverflow.com/questions/18512781/built-in-source-code-location – Henry Woody Dec 26 '18 at 08:08
  • 1
    "if Go team doesn't like optional parameter then why they had supported a function which is so frequently used with this anti-pattern?" That's a matter of opinion and is off-topic on Stack Overflow:. "primarily opinion-based : Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise." – peterSO Dec 26 '18 at 09:35
  • @peterSO Yes, that is certainly an opinionated question but my main question isn't that. I agree it can be considered a duplication of [Built-In source code location](https://stackoverflow.com/questions/18512781/built-in-source-code-location). – Aziz Dec 26 '18 at 13:30

1 Answers1

2

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.

Peter
  • 29,454
  • 5
  • 48
  • 60