How can I close bufio.Reader or bufio.Writer in golang?
bufio.Reader
bufio.Writer
func init(){ file,_ := os.Create("result.txt") writer = bufio.NewWriter(file) }
Should I close Writer ? or just use file.Close() make Writer close?
Writer
file.Close()
As far as I know, you cannot close bufio.Writer .
What you do is Flush() bufio.Writer and then Close() os.Writer :
Flush()
Close()
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 }