Ivan
Ivan

Reputation: 1851

Function declaration syntax in Go

What is (c App) in the following function declaration?

func (c App) SaveSettings(setting string) revel.Result {
--------------------------------------------------------------------------------------
func                                                      Keyword to define a function
     (c App)                                              ????
             SaveSettings                                 Function name
                         (setting string)                 Function arguments
                                          revel.Result    Return type

Upvotes: 7

Views: 2996

Answers (1)

twotwotwo
twotwotwo

Reputation: 30027

(c App) gives the name and type of the receiver, Go's equivalent of C++ or JavaScript's this or Python's self. c is the receiver's name here, since in Go it's conventional to use a short, context-sensitive name instead of something generic like this. See http://golang.org/ref/spec#Method_declarations --

A method is a function with a receiver. The receiver is specified via an extra parameter section preceeding the method name.

and its example:

func (p *Point) Length() float64 {
    return math.Sqrt(p.x * p.x + p.y * p.y)
}

Upvotes: 14

Related Questions