英文:
Is this type assertion ever valid?
问题
我正在阅读Go的io包的源代码,我遇到了一个我不完全理解的片段。在这里
func WriteString(w Writer, s string) (n int, err error) {
if sw, ok := w.(stringWriter); ok {
return sw.WriteString(s)
}
return w.Write([]byte(s))
}
其中
type stringWriter interface {
WriteString(s string) (n int, err error)
}
type Writer interface {
Write(p []byte) (n int, err error)
}
类型断言w.(stringWriter)
断言w的动态类型(即Writer)实现了stringWriter接口。鉴于stringWriter和Writer的类型定义,我不明白这是如何可能的。假设这段代码是正确的,我错过了什么?
英文:
I am reading the source code for Go's io package and I came across a snippet I don't fully understand. Here it is
func WriteString(w Writer, s string) (n int, err error) {
if sw, ok := w.(stringWriter); ok {
return sw.WriteString(s)
}
return w.Write([]byte(s))
}
where
type stringWriter interface {
WriteString(s string) (n int, err error)
}
type Writer interface {
Write(p []byte) (n int, err error)
}
The type assertion w.(stringWriter)
asserts that the dynamic type of w (i.e. Writer) implements the stringWriter interface. I don't see how this is possible given the type definitions of stringWriter and Writer. Assuming that this code is correct, what is it that I am missing?
答案1
得分: 5
你可以轻松地构建一个实现Writer
和stringWriter
的类型:
type StringWriter struct{}
func (s StringWriter) Write(in []byte) (int, error) {
return 0, nil
}
func (s StringWriter) WriteString(s string) (int, error) {
return s.Write([]byte(s))
}
因此,将StringWriter
的实例传递给io.WriteString
会调用StringWriter.WriteString
。
io.WriteString
的逻辑是:如果写入器具有WriteString
方法,则使用该方法,否则回退到string
到[]byte
的转换。为了测试实例是否实现了某个方法,使用接口,因为接口只是方法集,可以轻松地进行测试。
英文:
You can easily build a type which implements Writer
as well as stringWriter
:
type StringWriter struct{}
func (s StringWriter) Write(in []byte) (int, error) {
return 0, nil
}
func (s StringWriter) WriteString(s string) (int, error) {
return s.Write([]byte(s))
}
So passing an instance of StringWriter
to io.WriteString
results in StringWriter.WriteString
being called.
The logic behind io.WriteString
is: Use WriteString
on the writer if it has such a method
or fall back to string
to []byte
conversion otherwise. To test whether the instance implements
a method or not, interfaces are used, as these are just method sets and can be tested for easily.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论