Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameter before the function name in Go? [duplicate]

Tags:

go

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?

like image 373
typos Avatar asked Aug 11 '17 18:08

typos


People also ask

What is () before function name in Golang?

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.

How are parameters passed in Go?

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.

Does Golang have named arguments?

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 does func () mean in Golang?

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.


1 Answers

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.

like image 183
Rob Napier Avatar answered Sep 25 '22 00:09

Rob Napier