7

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
3
  • 1
    Instead of asking such questions I'd suggest skimming the short and easy to find Go Language Specification. IMO, questions and answers that do nothing more than just regurgitate parts of a readily available and more authoritative source aren't useful. Commented Jul 23, 2015 at 19:24
  • 2
    1) I wouldn't say that document is short at all. 2) I already looked at that document, but I was looking at function declarations, not method declarations, because I know nothing about Go: golang.org/ref/spec#Function_declarations 3) Actually everything can be found in more authoritative sources. I think this question may be of use to future people learning Go, and perfectly fits on Stack Overflow. Commented Jul 23, 2015 at 19:33
  • @Ivan, agreed, I was looking at Function_declarations too! And the doc isn't short. Commented Feb 22, 2017 at 18:34

1 Answer 1

14

(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)
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.