且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从io.Writer写的内容中读取内容

更新时间:2023-01-18 18:03:51

如果要在写入时捕获字节,请使用 io.MultiWriter bytes.Buffer 作为第二作者。

If you want to capture the bytes as they are written, use an io.MultiWriter with a bytes.Buffer as the second writer.

var buf bytes.Buffer
w := io.MultiWriter(codeFile, &buf)

或者在stdout上查看文件:

or to see the file on stdout as it's written:

w := io.MultiWriter(codeFile, os.Stdout)

否则,如果你想要重新读取同一个文件,你需要在写完后回头找:

Otherwise, if you want to re-read the same file, you need to seek back to the start after writing:

codeFile.Seek(0, 0)