英文:
Why is there no RuneWriter interface in Go's io package?
问题
Go的io
包包含(其中之一)以下接口:
type ByteReader interface {
ReadByte() (c byte, err error)
}
type ByteScanner interface {
ByteReader
UnreadByte() error
}
type ByteWriter interface {
WriteByte(c byte) error
}
type RuneReader interface {
ReadRune() (r rune, size int, err error)
}
type RuneScanner interface {
RuneReader
UnreadRune() error
}
但是没有RuneWriter
接口:
type RuneWriter interface {
WriteRune(r rune) (size int, err error)
}
为什么会缺少RuneWriter
接口呢?
英文:
Go's io
package contains (among others) the following interfaces:
type ByteReader interface {
ReadByte() (c byte, err error)
}
type ByteScanner interface {
ByteReader
UnreadByte() error
}
type ByteWriter interface {
WriteByte(c byte) error
}
type RuneReader interface {
ReadRune() (r rune, size int, err error)
}
type RuneScanner interface {
RuneReader
UnreadRune() error
}
But there is no RuneWriter
interface:
type RuneWriter interface {
WriteRune(r rune) (size int, err error)
}
Is there a reason that RuneWriter
is missing?
答案1
得分: 4
Go的作者根据需求来定义接口,而不是为了填充可能方法的网格而定义接口。这种策略有助于保持标准库的小巧和简单。
我认为他们得出的结论是,他们在标准包或其他他们维护的包中并不需要RuneWriter接口。
在Go团队之外,没有人对该接口提出需求。在问题跟踪器、邮件列表或可用的IRC频道历史记录中,都没有关于该接口的请求。
问题中提到的其他接口在标准包或其他Go作者维护的包中使用。
你可以在自己的包或代码中定义接口。这是一个非常有用的特性,而且在Go中有些独特。
英文:
The Go authors define interfaces based on need. They do not define an interface for the purpose of filling out a grid of possible methods. This policy helps to keep the standard library small and simple.
I think they concluded that there's little need for the the RuneWriter interface because they didn't need it in the standard packages or other packages that they maintain.
There's been no demand for the interface outside of the Go team. There are no requests for the interface on the issue tracker, mail list, or the available recorded history for the irc channel.
The other interfaces referenced in the question are used in the standard packages or other packages that the Go authors maintain.
You can define the interface in your own package or code. This is a very useful feature that's somewhat unique to Go.
答案2
得分: 2
对Be Bop的回答进行小小的补充:
接口在定义通用函数时非常有用。例如,func f(foo interface { Foo() })
表示“我是函数 f,如果你给我一个可以调用 Foo() 方法的东西,我会做好工作”。
现在考虑一下像 RuneScanner
这样的接口。RuneScanner
提供了一些非平凡的方法,特别是 UnreadRune
,这个方法不能通过“更低级”的方式轻松模拟。
RuneWriter
接口有什么好处呢?定义一个函数 func g(rw RuneWriter)
,它表示“给我一个可以写入符文的东西,我会完成我的工作!”?但实际上并没有真正的需要,因为这可以通过标准手段轻松模拟:将其定义为 func g(r io.Writer)
,然后在内部使用 fmt.Fprintf(r ...)
。
如果你想写入符文,你必须具备写入 1 到 6 个字节的能力,而这正是 io.Writer
提供的。所以不需要 RuneWriter
。
引入 RuneWriter
会使代码更易读或更安全吗?可能不会:函数 func g(rw RuneWriter)
明确表示它只希望向其参数写入 符文。这很好,但对于编写更好的程序来说并没有真正的帮助。符文是由 1 到 6 个字节组成的,g
做出的唯一额外承诺是“写入我的参数的任何内容都将是一个有效的 UTF-8 编码流”。这是一个非常肤浅的承诺。
英文:
Just a small addition to Be Bop's answer:
Interfaces are useful to define generic functions. E.g. func f(foo interface { Foo() })
states something like "I am function f and I'll do a proper job if you give me something I can Foo() with."
Now consider interfaces like RuneScanner
. A RuneScanner
provides nontrivial methods, especially UnreadRune
which cannot be easily simulated by "lower level" stuff.
What would a RuneWriter
interface be good for? Define a function func g(rw RuneWriter)
which announces itself as "Give me something I can write runes to and I'll do my job!" ? But there is no real need as this can be simulated trivialy by standard means: Define it as func g(r io.Writer)
and just use fmt.Fprintf(r ...)
inside.
If you want to write runes you have to have the ability to write 1 to 6 (?) bytes anyway and that is what an io.Writer
provides. So no need for a RuneWriter
.
Would it make code more readable or safer by introducing a RuneWriter
? Probably not: A function func g(rw RuneWriter)
clearly states that it only wishes to write runes to its argument. That's nice but not a really helpful for writing better programs. A rune is 1 to 6 bytes and the only additional promise that g
makes is "anything written to my argument will be a valid UTF-8 encoded stream". This is a very shallow promise.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论