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

How can I close bufio.Reader or bufio.Writer in golang?

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

Should I close Writer? or just use file.Close() will make Writer close?

As far as I know, you can't close the bufio.Writer.

What you do is to Flush() the bufio.Writer and then Close() the os.Writer:

writer.Flush()
file.Close()

I think the following is canonical:

func doSomething(filename string){
    file, err := os.Create(filename)
    // check err
    defer file.Close()
    writer = bufio.NewWriter(file)
    defer writer.Flush()

    // use writer here
}