英文:
What is (t *SimpleAsset) in this function
问题
func (t *SimpleAsset) Init(stub shim.ChaincodeStubInterface) peer.Response
我一直在尝试理解hyperledger
,其中我们使用Go
语言编写Chaincode
。但是我无法理解(t* SimpleAsset)
是什么意思。
我理解Init
是函数名,stub
是参数,peer.Response
是返回类型。由于我对Go
还不熟悉,请帮助我,谢谢。
英文:
func (t *SimpleAsset) Init(stub shim.ChaincodeStubInterface) peer.Response
I have been trying to understand hyperledger
in which we use Go
language for Chaincode
. But here I am unable to understand what the (t* SimpleAsset)
is.
I do understand that unit is the name of the function, stub part is the argument and peer.Response
is the return type. As I am new to Go
please help me thank you.
答案1
得分: 3
在下面的代码中:
func (t *SimpleAsset) Init(stub shim.ChaincodeStubInterface) peer.Response
(t *SimpleAsset)
是接收器。与许多其他语言不同,Go允许您向任何(用户定义的)类型(包括函数!)添加方法,并且您要添加方法的类型在此处被称为接收器。
请注意,这段代码的作者将接收器命名为t
,而不是像self
或this
这样的名称?在Go中,没有特殊的规则来命名接收器,您只需像命名参数一样命名它。
Go by example提供了一个很好的清晰解释基础知识,但是Go规范也非常有帮助。
英文:
In the following code:
func (t *SimpleAsset) Init(stub shim.ChaincodeStubInterface) peer.Response
(t *SimpleAsset)
is the receiver. Go, unlike many other languages allows you to add methods to any (user defined) type (including functions!), and the type you are adding the method to is refered to here.
Notice that the author of this code names his receiver t
instead of something like self
or this
? In Go there is no special rule for naming a receiver, you just name it like you would a parameter.
Go by example has a nice clear explanation of the basics, but the Go specification is also very helpful.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论