gpt4 book ai didi

function - 你能解释一下 func 关键字和函数名之间的参数吗?

转载 作者:IT王子 更新时间:2023-10-29 02:29:59 27 4
gpt4 key购买 nike

我是这门语言的新手,到目前为止我所读到的内容并不能解释这里发生的事情。

我在看 JSON decoding .

这是包中的一个函数:

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

这里有类似的表示法:

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

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

最佳答案

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

reader := decoder.Buffered()

看看 the Go tutorial lesson关于这个话题。

关于function - 你能解释一下 func 关键字和函数名之间的参数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32060348/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com