简体   繁体   English

惯用缓冲os.Stdout

[英]Idiomatically buffer os.Stdout

os.Stdout.Write() is an unbuffered write. os.Stdout.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? 是否有更惯用的方式来获得缓冲输出?

No, that is the most idiomatic way to buffer writes to Stdout. 不,这是缓冲写入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. 这将确保从函数返回时刷新缓冲区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM