Go 中的函数声明语法
Function declaration syntax in Go
下面函数声明中的(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)
给出 receiver 的名称和类型,Go 等价于 C++ 或 JavaScript 的 this
或 Python 的 self
。 c
是接收者的名字,因为 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.
及其示例:
func (p *Point) Length() float64 {
return math.Sqrt(p.x * p.x + p.y * p.y)
}
下面函数声明中的(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)
给出 receiver 的名称和类型,Go 等价于 C++ 或 JavaScript 的 this
或 Python 的 self
。 c
是接收者的名字,因为 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.
及其示例:
func (p *Point) Length() float64 {
return math.Sqrt(p.x * p.x + p.y * p.y)
}