函数声明语法:函数名称前括号中的内容 [英] Function declaration syntax: things in parenthesis before function name

查看:150
本文介绍了函数声明语法:函数名称前括号中的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,我无法在问题标题中更加具体,但是我正在阅读一些开始代码并且我遇到了这种形式的函数声明:

I'm sorry I couldn't be more specific in the question title, but I was reading some Go code and I encountered function declarations of this form:

func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    ...
}

来自 https://github.com/mattermost/platform/blob /master/api/context.go

func (s *GracefulServer) BlockingClose() bool {
    ...
}

来自 https://github.com/braintree/manners/blob/master /server.go

括号之间的(h handler)(s *GracefulServer)是什么意思?考虑到括号之间事物的含义,整个函数声明是什么意思?

What does the (h handler) and the (s *GracefulServer) between parenthesis mean? What does the entire function declaration mean, taking into account the meaning of the things between parenthesis?

这不是的副本Go中的函数和方法的概念?:之所以问这个问题,是因为我不知道函数名称之前的括号中的内容是什么,不是因为我想知道函数和方法之间的区别是什么...如果我知道此声明是我首先不会遇到这个问题的方法.如果有一天有人和我有同样的疑问,我不相信她会去寻找"golang方法".因为她不知道是这种情况.这就像想知道字母"sigma"是什么一样.表示在数学表达式之前(不知道表示求和),然后有人说这是求和与其他事物之间的区别的副本.

This is not a duplicate of Whats the difference of functions and methods in Go? : this question came to me because I didn't know what the things in parenthesis before the function name were, not because I wondered what was the difference between functions and methods... if I knew that this declaration was a method I wouldn't have had this question in the first place. If someone has the same doubt as me one day, I don't believe she will go searching for "golang methods" because she doesn't know that this is the case. It would be like wondering what the letter "sigma" means before a mathematical expression (not knowing it means summation) and someone says it's a duplicate of what's the difference between summation and some other thing.

此外,对该问题的简短回答(这是一个接收者")不是对函数和方法之间的区别是什么"的答案.

Also, the short answer to this question ("it's a receiver") is no answer to "what's the difference between functions and methods".

推荐答案

这称为接收器".在第一种情况下,(h handler)是值类型,在第二种情况下,(s *GracefulServer)是指针. Go的工作方式可能与某些其他语言有所不同.但是,在大多数面向对象的编程中,接收类型或多或少地像一个类.这是您从中调用方法的事情,就像如果我将某些方法A放在某个类Person的旁边,那么我将需要一个类型为Person的实例才能调用A(假设它是一个实例)方法,而不是静态的!).

This is called the 'receiver'. In the first case (h handler) it is a value type, in the second (s *GracefulServer) it is a pointer. The way this works in Go may vary a bit from some other languages. The receiving type however, works more or less like a class in most object-oriented programming. It is the thing you call the method from, much like if I put some method A in side some class Person then I would need an instance of type Person in order to call A (assuming it's an instance method and not static!).

此处的陷阱是,接收方像其他参数一样被推入调用堆栈,因此,如果接收方是值类型(例如handler的情况),则您将处理被称为对象的副本.返回调用范围后,表示h.Name = "Evan"之类的方法将不再存在.由于这个原因,任何希望改变接收器状态的事物都需要使用指针或返回修改后的值(如果需要,则可以提供更多的不可变类型范例).

One gotcha here is that the receiver gets pushed onto the call stack like other arguments so if the receiver is a value type, like in the case of handler then you will be working on a copy of the thing you called the method from meaning something like h.Name = "Evan" would not persist after you return to the calling scope. For this reason anything that expects to change the state of the receiver, needs to use a pointer or return the modified value (gives more of an immutable type paradigm if you're looking for that).

这是规范中的相关部分; https://golang.org/ref/spec#Method_sets

Here's the relevant section from the spec; https://golang.org/ref/spec#Method_sets

这篇关于函数声明语法:函数名称前括号中的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆