惯用的缓冲os.Stdout [英] Idiomatically buffer os.Stdout

查看:26
本文介绍了惯用的缓冲os.Stdout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

os.Stdout.Write()无缓冲写入.要获得缓冲写入,可以使用以下代码:

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)

问题:

是否还有一种惯用的方式来获取缓冲输出?

Is there a more idiomatic way to get buffered output?

推荐答案

不,这是缓冲对Stdout写入的最惯用的方法.在许多情况下,您还需要添加一个延迟:

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.

这篇关于惯用的缓冲os.Stdout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆