Can you explain parameters between func keyword and function name?

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

Can you explain parameters between func keyword and function name?

问题

我是你的中文翻译助手,以下是翻译好的内容:

我对这门语言还不熟悉,到目前为止,我所读的内容并没有解释这里发生了什么。

我在查看JSON解码

这是该包中的一个函数:

func (dec *Decoder) Decode(v interface{}) error

这里也有类似的表示法:

func (dec *Decoder) Buffered() io.Reader

这些函数如何访问(dec *Decoder)?考虑到第二个函数没有参数,我猜想这不是必须直接在函数调用中传递的,而是类似的东西?

英文:

I'm new to the language and so far what I have read doesn't explain what is happening here.

I was looking at JSON decoding.

Here is a function in the package:

func (dec *Decoder) Decode(v interface{}) error

There is similar notation here:

func (dec *Decoder) Buffered() io.Reader

How do these functions access the (dec *Decoder)? Given that the second function has no parameters, I am guessing this is not something that must be passed directly in the function call but something similar?

答案1

得分: 4

这两个函数是方法,因为它们与接收器相关联。在每个方法声明中,(dec *Decoder)描述了接收器。如果你有一个名为decoder*Decoder,你可以像这样调用它的Buffered方法:

reader := decoder.Buffered()

请参考Go教程中的这个主题

英文:

These two functions are methods because they are associated with receivers. In each method declaration, (dec *Decoder) describes the receiver. If you have a *Decoder called decoder, you call Buffered on it like this:

reader := decoder.Buffered()

Take a look at the the Go tutorial lesson on this topic.

huangapple
  • 本文由 发表于 2015年8月18日 05:55:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/32060348.html
匿名

发表评论

匿名网友

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

确定