bufio.Reader ReadRune – size 0(返回值)是否可能?

huangapple go评论71阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2016年3月16日 05:12:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/36022253.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定