Do I need to create a buffer for the item to be written to before writing to file? I have found so many ways to create a file and write to it that I am not sure what is really taken placing and going on.
I would be appreciative if someone can someone offer a simple Python -to- Golang examples that I can work through and discuss?
I not sure to be honest. I am new to a buffer concept since in TCL and Perl you can write the variable/scalar directly to the file handler.
However, I hoped to find something similar in Go that I can use to write a variable directly to a file handler without having to worry about the buffer and the number of bytes stored in the buffer.
Also, I want to say - if you walk me through the buffer concept and concepts⌠I would be glad to learn how the buffer works and move forward with using a better approach to problems in coding.
Actually, you can do the same in Go like you do in Perl. To do that, you need to open the file to get a handler for the file first, like so (itâs actually almost the same example in go by example site):
f, err := os.OpenFile(file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic(err) // i'm simplifying it here. you can do whatever you want.
}
defer f.Close()
Then, you can just write a string to the file like so:
ioutil.WriteFile is used when you want to write to a file once.
It creates or truncates the file, you canât append).
os.Create is like os.OpenFile
It creates a file or truncates it if it exists and gives you a file handler
It calls OpenFile behind the scenes like so: OpenFile(filename, O_RDWR|O_CREATE|O_TRUNC, 0666)
f.Write is used when you want to write to the file using a byte slice.
f.WriteString is used when you want to write to the file using a string value
bufio.NewWriter is used when you want to write to a file successively in an efficient manner.
Itâs because, it buffers your Write calls (it buffers them in memory), then at some points, it actually writes the accumulated buffer to the file. So, it doesnât write each time you write, it writes in batches.
Hi Inancgumus,
Thank you for this example - this puts Go code concepts writing strings to a file into perspective for me; however, would you mind showing a simple writing with a buffer concept, so I can start thinking in better programming concepts and skills?
I still feel as if I am missing something with concepts, which I would normally not think of or concepts that are hidden in the background. I am working through the book Go in Action by William Kennedy from Manning publishing but it is slow going, yet I am just itching to get immersed more into Go.
To be honest I am not sure where to start. Do you have a source or links that will provide insight into using a Buffer? Also, when do you know when you should use a buffer?
Thank you both for your code review. In addition, @inancgumus thank you for showing the âbufferâ example, which keeps me thinking in buffer concepts, but also it was very helpful getting the two different versions.
Your solutions worked, but I wanted to continue to toy with this.
So I was experimenting with the code and started to try some different things, so I can learn and understand what is taking place here. Starting slow, and trying to understand the TYPE of objects that are getting returned. Due to the latter, I used the following code
fmt.Printf("%T\n",g)
I noticed I received the following: â[]uint8â, which I was not expecting. Still I found the following code, from another online forum:
:= string([]byte(c[:]))
From my research - âcâ was getting returned as a byte array, not sure if âarrayâ is the right word; however, I realized that I needed to convert the []uint8 into string, which the above code did.