I'm confused by the structure of the following Golang code:
type Team []*athlete
func (s Team) Len() int {
//some code here
}
func (s Team) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
I'm new to go and not familiar with this function declaration structure. What are the input/output values? Apologies for what I'm sure is a naive question. Tried to google, referenced my Go book, and still confused.
In the declaration func (r Thing) Name(variable aType) otherType
, the various things are (in order):
func
is the "this is a function" keyword(r Thing)
indicates "this is a method on the type Thing
, the value the method is invoked on will be in the variable r
" (normal functions do not have a receiver; methods always have exactly one receiver).Name
is the name of the method(variable aType)
is an argument list, in this case a single argument, of the type aType
. It is perfectly valid to have zero arguments to a method.otherType
is the return type, this can be omitted, if no useful return value exists.