英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论