php小编草莓在这篇文章中将为大家介绍io.Reader与fmt.Fscan无限循环的问题。在使用fmt.Fscan函数读取输入时,如果读取的内容与输入的格式不匹配,会导致无限循环的情况发生。这个问题可能会给我们带来很大的困扰,但是通过一些技巧和注意事项,我们可以轻松解决这个问题。接下来,我们将详细讲解如何避免io.Reader与fmt.Fscan无限循环的情况,以帮助大家更好地使用这两个函数。
问题内容
不知道为什么,但我的 io.reader 实现似乎存在某种缺陷?
io.reader 的文档指出返回非零字节计数和非零错误应该没问题:
it may return the (non-nil) error from the same call or return the error (and n == 0) from a subsequent call. an instance of this general case is that a reader returning a non-zero number of bytes at the end of the input stream may return either err == eof or err == nil. the next read should return 0, eof.callers should always process the n > 0 bytes returned before considering the error err. doing so correctly handles i/o errors that happen after reading some bytes and also both of the allowed eof behaviors.
登录后复制
但这对 fmt.fscan 不起作用,而是挂起程序:
package mainimport ( "fmt" "io")type byte bytefunc (b byte) read(p []byte) (n int, err error) { if len(p) == 0 { return 0, io.errshortbuffer } p[0] = byte(b) return 1, io.eof}func main() { var n int b := byte('9') z, err := fmt.fscan(b, &n) fmt.println(n, z, err)}
登录后复制
当然,如果我单独使用 io.eof 返回零字节计数,它会起作用:
type Byte struct { v byte eof bool}func (b *Byte) Read(p []byte) (n int, err error) { if len(p) == 0 { return 0, io.ErrShortBuffer } if b.eof { return 0, io.EOF } p[0] = b.v b.eof = true return 1, nil}func main() { var n int b := Byte{v: '9'} z, err := fmt.Fscan(&b, &n) fmt.Println(n, z, err)}
登录后复制
我原来的实现中是否存在缺陷,或者我不应该依赖于 io.reader 的这一特定记录行为,并且在没有更多数据可供读取时始终单独返回 0, io.eof ?
解决方法
fmt.scanf 确实正确处理返回计数和 io.eof,但即使在 io.eof 之后,您的阅读器仍继续返回值。
由于扫描器实现依赖于使用 io.readfull,而后者使用 io.readatleast,因此您将需要一个更完整的实现来处理重复读取。您可以通过使用跟踪 eof 的扩展版本来测试这一点,并在第一个 read 上返回 io.eof ,它仍然可以按预期与 fmt.fscan 一起使用。
文档的主要摘录:
io.readfull:
…它不会将 read 中的 eof 视为要报告的错误
io.readatleast:
仅当未读取任何字节时,错误才会为 eof。
因为这些 io 帮助程序需要自己解释 io.eof ,所以它们的调用者只能查找返回的实际数据,并且由于您的阅读器继续返回数据,它们将无限期地重复调用。通过在阅读器上重复调用 io.readall 可以轻松演示这一点,每次都会返回另一个值。
b := Byte('9')fmt.Println(io.ReadAll(b))fmt.Println(io.ReadAll(b))fmt.Println(io.ReadAll(b))// [57] // [57] // [57]
登录后复制
https://www.php.cn/link/ad6fff7b7be06acff1c63ced9f0da4ea
以上就是io.Reader 与 fmt.Fscan 无限循环的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2356787.html