I'm confused when I should actually be using bufio package over the ioutil. for example when writing a file or reading a file. I have the scenario where there are multiple functions and APIs that process on the same data stage by stage. I'm not sure if choosing bufio in this case over ioutil helps? please suggest.
The intention of the bufio package is what it states (https://golang.org/pkg/bufio/) - implements buffered I/O. So for writing, if you don't flush, data will remain in the buffer as indicated in this example. Bufio's Write also requires an object which implements the Writer interface.
Whereas ioutil doesn't have buffering etc. - you write directly to the named file, without having to open it, like:
myData := []byte("Testing
go
")
err := ioutil.WriteFile("/tmp/data1", myData, 0644)
So as one usecase, if you have all of your data ready and need to write to a file - just one-time, then ioutil is a convenient choice.
However if your data gets generated as your code progresses, then bufio is a more suitable option, you can use WriteString as many times as you need and then finally call flush.
Similarly for reading, for ioutil, the Read methods read the entire data all at once, this may not be suitable for very large files, but could be desirable/acceptable in some other cases. Whereas bufio provides you methods where you have more control on how much data you want to read, it provides helpful methods for reading line by line, split by some other token etc.
Here's a program on playground which illustrates writes using both these packages.
If you have all the data prepared, that WriteFile is easier to use (high level API). io.Writer is more general. It is interface, that allows you for example
If you don't need such things WriteFile is easier to read. If you need something more sophisticated, you have to use io.Writer. But code is much longer - you have to open a file (+check err) and close it.