英文:
Go Programming Language Interface Conceptual Understanding
问题
我正在编写一个示例的Http客户端/服务器代码,以便更多地了解Go。在编写过程中,我需要读取响应的Body。所以我查看了Go Pkg文档http://golang.org/pkg/net/http/#Response,并看到Body io.ReadCloser,即Body的类型是io.ReaderCloser。为了知道io.ReaderCloser是什么,我点击了超链接,看到http://golang.org/pkg/io/#ReadCloser是一个实现了两个方法Reader和Closer的接口。看到这个,我以为我可以这样做:
line, _ , err := response.Body.Reader.ReadLine()
然后Go编译器给我报错说response.Body中没有Reader方法或类型。我觉得在概念上我对为什么不能这样做有问题。也许我过于按照Java的思维方式思考了。如果有人能指出我的误解,我将不胜感激。
谢谢
英文:
I am writing a sample Http Client/Server code to learn more about Go. During the process of writing I needed to read the response Body. So I looked at the Go Pkg Documentation http://golang.org/pkg/net/http/#Response and saw that Body io.ReadCloser i.e. Body is of type io.ReaderCloser. In order to know what is io.ReaderCloser, I clicked on the hyperlink and saw that http://golang.org/pkg/io/#ReadCloser is an interface that implements two methods Reader and Closer. Looking at this I thought I could do this
line, _ , err := response.Body.Reader.ReadLine()
Go compiler then gave me the error there is no method or type Reader in response.Body. I think conceptually I am having a problem of understanding why I could not do this. Maybe I am thinking too much in java terms. I would appreciate if somebody could point out my misunderstanding.
Thanks
答案1
得分: 5
TLDR
Body是一个Reader。它不包含一个Reader。只需直接在Body上调用Reader方法。
详细信息
对Go接口的10秒简介:一组类型必须实现的方法,如果它具有所有方法,则实现接口,没有其他情况。
接下来是刚刚让你困扰的嵌入。如果我们有一个接口Foo
type Foo interface {
FooIt() error
}
并且我们想要另一个接口也有方法FooIt +一些其他东西,我们可以像这样“嵌入”接口。
type FooPlusPlus interface {
Foo // 这将Foo嵌入FooPlusPlus,使得Foo的所有方法都成为FooPlusPlus的方法池的一部分。
FooItAll() (bool, error)
}
嵌入提供了一种组合相关接口的好方法,就像Reader和Closer一样。你可以有点像在Java中继承。你不需要显式地要求嵌入的接口,就像你在Java中的子类中不需要要求父类一样。
现在来实现FooPlusPlus
type Demo int
func (_ *Demo) FooIt() error {return nil}
func (_ *Demo) FooItAll() (bool, error) {return false, nil}
然后,现在Demo是一个FooPlusPlus。
英文:
TLDR
Body is a Reader
. It doesn't contain one. Just call Reader
methods directly on Body
.
Gory Details
10 second intro to Go interfaces: A set of methods that types must implement, if it has all the methods, it implements the interface, period.
Next is what just bit you, embedding. If we have an interface Foo
type Foo interface {
FooIt() error
}
and we want another interface also with the method FooIt
+ some stuff, we can "embed" the interface like this.
type FooPlusPlus interface {
Foo // This embeds Foo in FooPlusPlus, making all of Foo's methods
// part of FooPlusPlus's method pool.
FooItAll() (bool, error)
}
Embedding provides a nice way to compose related interfaces, like Reader
and Closer
. You can kinda think of it like inheriting in Java land. You don't explicitly ask for the embedded interface any more than you'd ask for a parent class in Java with a subclass.
Now to implement FooPlusPlus
type Demo int
func (_ *Demo) FooIt() error {return nil}
func (_ *Demo) FooItAll() (bool, error) {return false, nil}
And viola, now Demo is a FooPlusPlus
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论