简体   繁体   English

为什么Go的io包中没有RuneWriter接口?

[英]Why is there no RuneWriter interface in Go's io package?

Go's io package contains (among others) the following interfaces: 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
}

But there is no RuneWriter interface: 但是没有RuneWriter接口:

type RuneWriter interface {
        WriteRune(r rune) (size int, err error)
}

Is there a reason that RuneWriter is missing? 缺少RuneWriter是有原因的吗?

The Go authors define interfaces based on need. Go作者根据需要定义接口。 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. 我认为他们得出的结论是,几乎不需要RuneWriter接口,因为他们在标准软件包或他们维护的其他软件包中都不需要它。

There's been no demand for the interface outside of the Go team. Go团队之外对接口没有任何需求。 There are no requests for the interface on the issue tracker, mail list, or the available recorded history for the irc channel. 在问题跟踪器,邮件列表或irc通道的可用记录历史记录上均没有对该接口的请求。

The other interfaces referenced in the question are used in the standard packages or other packages that the Go authors maintain. 问题中引用的其他接口在Go编写者维护的标准软件包或其他软件包中使用。

You can define the interface in your own package or code. 您可以在自己的程序包或代码中定义接口。 This is a very useful feature that's somewhat unique to Go. 这是一个非常有用的功能,对于Go来说有些独特。

Just a small addition to Be Bop's answer: 只是Be Bop的答案的一小部分:

Interfaces are useful to define generic functions. 接口对于定义通用功能很有用。 Eg 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." 例如func f(foo interface { Foo() })表示类似“我是函数f,如果您给我可以使用Foo()的东西,我会做适当的工作。”

Now consider interfaces like RuneScanner . 现在考虑像RuneScanner这样的RuneScanner A RuneScanner provides nontrivial methods, especially UnreadRune which cannot be easily simulated by "lower level" stuff. RuneScanner提供了非常简单的方法,尤其是UnreadRune ,这些方法无法通过“较低级别”的内容轻松模拟。

What would a RuneWriter interface be good for? RuneWriter界面有什么RuneWriter 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!" 定义一个函数func g(rw RuneWriter) ,该函数将自身声明为“给我一些我可以写符文的东西,我会做的!” ? 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. 但是并没有真正的需求,因为可以通过标准方法对此进行简单地模拟:将其定义为func g(r io.Writer)并在其中使用fmt.Fprintf(r ...) 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. 如果要编写符文,则无论如何都必须能够写入1到6(?)字节,这就是io.Writer提供的。 So no need for a RuneWriter . 因此,不需要RuneWriter

Would it make code more readable or safer by introducing a RuneWriter ? 通过引入RuneWriter它会使代码更易读或更安全吗? Probably not: A function func g(rw RuneWriter) clearly states that it only wishes to write runes to its argument. 可能不是:函数func g(rw RuneWriter)明确声明它只希望向其参数写入符文 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". 符文为1到6个字节,并且g做出的唯一附加承诺是“写入我的参数的任何内容将是有效的UTF-8编码流”。 This is a very shallow promise. 这是一个很浅薄的承诺。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM