We’ve just finished up the rather long section on different types of statements. There’s only a few more sections in the spec, before we finish this series.
And today we start looking at Built-in functions!
Built-in functions
Built-in functions are predeclared. They are called like any other function but some of them accept a type instead of an expression as the first argument.
The built-in functions do not have standard Go types, so they can only appear in call expressions; they cannot be used as function values.
If you’ve ever wondered why there are some seemingly arbitrary limitations on how built-in functions can be used, this answers it.
myAppend := append
will fail to compile, with the following error:
./prog.go:6:14: append (built-in) must be called
The reason this isn’t allowed is that built-in functions simply don’t have a defined type, therefore myAppend
, in the example, has an undefined type. And there’s simply no way to handle that.
If you want to reference a built-in as a variable like this, you must wrap it in your own function:
myAppend := func(a []string, b ...string) []string {
return append(a, b...)
}
Quotes from The Go Programming Language Specification Language version go1.23 (June 13, 2024)