如何在golang中关闭bufio.Reader/Writer?

MrR*_*ROY 13 go

我怎么能关闭bufio.Readerbufio.Writer在golang?

func init(){
    file,_ := os.Create("result.txt")
    writer = bufio.NewWriter(file)
}
Run Code Online (Sandbox Code Playgroud)

我应该关闭Writer吗?或者只是使用file.Close()Writer关闭?

ANi*_*sus 20

据我所知,你无法关闭bufio.Writer.

你做的是Flush()bufio.Writer,然后Close()os.Writer:

writer.Flush()
file.Close()
Run Code Online (Sandbox Code Playgroud)