惯用的缓冲os.Stdout
问题描述:
os.Stdout.Write()
is an unbuffered write. To get a buffered write, one can use the following:
f := bufio.NewWriter(os.Stdout)
f.Write(b)
Question:
Is there a more idiomatic way to get buffered output?
问题: strong> p>
是否有更惯用的方式来缓冲 em>输出? p>
div> os.Stdout.Write() code>是无缓冲 em> 写。 要获得缓冲 em>写入,可以使用以下代码: p>
f:= bufio.NewWriter(os.Stdout)
f.Write(b )
code> pre>
答
No, that is the most idiomatic way to buffer writes to Stdout. In many cases, you will want to do also add a defer:
f := bufio.NewWriter(os.Stdout)
defer f.Flush()
f.Write(b)
This will ensure that the buffer is flushed when you return from the function.