简体   繁体   English

Go中的函数声明语法

[英]Function declaration syntax in Go

What is (c App) in the following function declaration? 以下函数声明中的(c App)是什么?

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

(c App) gives the name and type of the receiver , Go's equivalent of C++ or JavaScript's this or Python's self . (c App)给出接收器的名称和类型,Go相当于C ++或JavaScript的this或Python的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 . c在这里是接收者的名字,因为在Go中传统上使用一个简短的,上下文相关的名称而不是像this通用名称 See http://golang.org/ref/spec#Method_declarations -- 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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM