I am working on the project in which I need to generate a file as binary encoded(bytes). Thing is that, I don't want to store the file but just need the encoded data to verify with some test data.
Here is the sample code I have zip file
Above example is for zip
but I am not restricted to use zip
or any file type. And here we need to have stored file to read but I don't want to store and delete the file later.
I know it has not much efforts to ask for the help, but as an newbie, I come to that only.
Thanks.
A better practice is to not rely on concrete types (e.g. os.File
), but use interfaces instead that describe the functionality you want to use the file for.
E.g. if you have a function that takes a file because it wants to read from it, use io.Reader
instead:
func process(r io.Reader) error {
// ...
}
Similarly, if you want to write to the file, use io.Writer
, or if you want to do both, use io.ReadWriter
or io.ReadWriteCloser
. You may pass an *os.File
value to these functions, because *os.File
implements those interfaces.
The benefit of this is that you can call these functions with any values that implement the interface. If you want to test these functions, you may pass an in-memory bytes.Buffer
which implements io.Reader
and io.Writer
, and whose content you can construct manually, at runtime, for example:
buf := &bytes.Buffer{}
buf.Write([]byte{1, 2, 3})
buf.WriteString("Hello")
Here buf
will contain the bytes 1
, 2
, 3
and the string "Hello"
. After that you may pass buf
where a reader or writer is needed, e.g.:
process(buf)
See similar / related questions and examples: