Go function declaration syntax

What is (c App) in the next 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 
+7
function syntax go
source share
1 answer

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

A method is a function with a receiver. The receiver is specified through an additional parameter section preceding the method name.

and his example:

 func (p *Point) Length() float64 { return math.Sqrt(px * px + py * py) } 
+14
source share

All Articles