I have seen some Go functions defined like this:
type poly struct {
coeffs [256]uint16
}
func (p *poly) reset() {
for i := range p.coeffs {
p.coeffs[i] = 0
}
}
Which you can later call as:
var p poly
p.reset()
I haven't seen this in other programming languages that I know. What's the purpose of p *poly
in the reset function? It seems to be like a function parameter but written before the function name. Any clarification for it?
The parenthesis before the function name is the Go way of defining the object on which these functions will operate. So, essentially ServeHTTP is a method of type handler and can be invoked using any object, say h, of type handler.
Golang supports two different ways to pass arguments to the function i.e. Pass by Value or Call by Value and Pass By Reference or Call By Reference. By default, Golang uses the call by value way to pass the arguments to the function.
In Golang, Named Return Parameters are generally termed as the named parameters. Golang allows giving the names to the return or result parameters of the functions in the function signature or definition. Or you can say it is the explicit naming of the return variables in the function definition.
What is Function in Golang. A function is a group of statements that exist within a program for the purpose of performing a specific task. At a high level, a function takes an input and returns an output. Function allows you to extract commonly used block of code into a single component.
It means that reset()
is a method on *poly
. This is very basic Go; you really need to start with the Go tour. Trying to read Go without a basic understanding of its syntax is going to be very confusing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With