最近再练手精进 网上弄了段代码 运行发型 bufio.Buffered()永远返回 0
package main import ( "bufio" "fmt" "strings" ) func main() { //原始字符串 str := "12345678901234567890123456789012345678901234567890" //将字符串转换为流式 I/O 对象 strReader := strings.NewReader(str) //设置读缓存区大小 bufReader := bufio.NewReaderSize(strReader, 16) //16byte //缓存读 p := make([]byte, 4) n, err := bufReader.Read(p) if err != nil { panic(err) } buffered := bufReader.Buffered() fmt.Printf("buffered:%d, content:%s\n", buffered, p[:n]) } 网上搜来搜去只返回一个结果
13 年前有人 report 过 4 年前也有人遇到过 到现在是不是还没解决?
https://groups.google.com/g/golang-nuts/c/1smBsPOdFT0
I'm having a similar problem as the original poster. I expect Buffered() to return the number of bytes that can be read from the current buffer, like the documentation states. However a call to Buffered() returns 0 both before and after a call to any Read() that finds bytes in the buffer. In my case the Reader is a net.Conn.
How do we use Buffered() properly? This is the only google result I could find that even mentions using the function.
I ended up solving my problem without using bufio. -Sam
