In Python, lists offer an append()
method which can be called using standard Python method syntax; for example:
>>> my_list = []
>>> my_list.append('a', 'b', 'c')
>>> my_list
['a', 'b', 'c']
This syntax is intuitive to me, and this pattern is common in list implementations across many OO languages.
Lately, I've been working more closely with Go, and I'm trying to get a better understanding of some of the design choices made in the language. Most notably, why is append()
defined as a regular function (taking the slice to append to as the first argument, and returning the newly-constructed slice), rather than a method on the slice type?
I'm aware that Go doesn't have a self
/this
construct, but the standard library takes some liberties with how its functions are defined with respect to the rest of the language; for example, make()
takes a type as a first argument, and this is, as far as I know, not possible when defining a new function.
Is it just a nod to C's pattern of passing references to variables, rather than returning new variables?