On the document, API shows make
accepts a Type and a variable-size IntegerType parameter.
func make(t Type, size ...IntegerType) Type
To make an array, I can pass 3 parameters, such as make([]int, 3, 5)
but when I try to make a map make(map[int]int, 3, 5)
it pops too many arguments to make(map[int]int)
when I compile.
Is this something related to the compiler? And is it possible to implement this behaviour for my own functions?
The compiler has special knowledge of make
and other built-in functions.
The compiler enforces the allowed argument count for kind of value that make
is initializing. The compiler will not enforce argument counts for user defined functions. What's more, user defined functions cannot have a type as an argument.
The best you can do is check at runtime:
func example(a ArgType, size ...int) returnType {
if len(size) != 2 {
panic("expected two size values")
}
...
}