bufio.Buffered 返回 0 一个未解决 bug - V2EX
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
zzzkkk

bufio.Buffered 返回 0 一个未解决 bug

  •  
  •   zzzkkk Sep 10, 2023 1801 views
    This topic created in 977 days ago, the information mentioned may be changed or developed.

    最近再练手精进 网上弄了段代码 运行发型 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

    7 replies    2023-09-11 11:25:08 +08:00
    klesh
        1
    klesh  
       Sep 10, 2023
    啥版本?似乎 v1.20+ 没这个问题 https://go.dev/play/p/0Iqldy6RqNY?v=goprev
    darrh00
        2
    darrh00  
       Sep 10, 2023
    输出:buffered:12, content:1234

    不是 12 吗,哪是 0 ?
    Jooeeee
        3
    Jooeeee  
       Sep 10, 2023
    试着跑了一下:
    buffered:12, content:1234
    ranleng
        4
    ranleng  
       Sep 10, 2023
    go version
    go version go1.20.6 darwin/arm64
    go run main.go
    buffered:12, content:1234
    thinkerDev
        5
    thinkerDev  
       Sep 10, 2023
    go 1.18 输出: buffered:12, content:1234
    dif
        6
    dif  
       Sep 11, 2023
    和楼上一样:
    go version
    warning: GOPATH set to GOROOT (D:\devtools\go) has no effect
    go version go1.19.5 windows/amd64

    buffered:12 ,context:1234
    dif
        7
    dif  
       Sep 11, 2023
    所以,建议这种问题先把版本号发出来
    About     Help     Advertise     Blog     API     FAQ     Solana     4085 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 46ms UTC 05:09 PVG 13:09 LAX 22:09 JFK 01:09
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86