英文:
bufio.Reader ReadRune - size 0 (return value) possible?
问题
当错误为nil时,ReadRune函数返回一个大小为0的符文的情况是当输入流已经结束时。这意味着没有更多的数据可供读取,因此返回一个大小为0的符文。这种情况下,错误为nil,因为这不是一个错误,只是表示已经到达了输入流的末尾。
英文:
Can ReadRune really ever have a size 0 return value when an error is nil?
I'm curious because I've seen some online examples with the following code:
//assuming input = *bufio.Reader
r, size, err := input.ReadRune()
if size == 0 && err == nil {
return 0, nil
} else if err != nil {
return 0, err
}
return r, nil
However, according to the go documentation:
> If the encoded rune is invalid, it consumes one byte and returns unicode.ReplacementChar (U+FFFD) with a size of 1.
So in what case would a size 0 rune be returned when the error is nil?
答案1
得分: 1
bufio.Reader的ReadRune方法不会返回size == 0且err == nil的情况。
该方法在无法读取有效的rune时,会读取一个rune或单个字节。在这两种情况下,返回的size都大于零。
英文:
There is no case where the bufio.Reader ReadRune method returns size == 0 and err == nil.
The method reads a rune or a single byte in the case where a valid rune cannot be read. In both cases, the size returned is greater than zero.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论