Function Closure
Methods
A method is just a function with a receiver argument.
You can only declare a method with a receiver whose type is defined in the same package as the method. You cannot declare a method with a receiver whose type is defined in another package (which includes the built-in types such as int
).
You can declare methods with pointer receivers.
Go interprets the statement v.Scale(5)
as (&v).Scale(5)
since the Scale
method has a pointer receiver.
Variadic Parameter
- Defining Variadic Functions: When defining a function, the
...
operator allows you to specify that the function can take any number of arguments of a specified type. This is similar to variadic functions in other programming languages like C’sprintf
or Python’s*args
. - Passing Arguments to Variadic Functions: When calling a variadic function, you can use the
...
operator to pass a slice as multiple arguments. TheAppend
function is a variadic function that can take multiple integers as arguments. By writingslice2...
, you are essentially spreading out the elements ofslice2
as individual arguments to theAppend
function.