简体   繁体   中英

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

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?

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.

Just a small addition to Be Bop's answer:

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."

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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