(t *SimpleAsset)在这个函数中是什么意思?

huangapple go评论85阅读模式
英文:

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,而不是像selfthis这样的名称?在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.

huangapple
  • 本文由 发表于 2017年8月27日 20:17:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/45904865.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定