How to read and write binary files?

In my previous programming language, it was easy to write structures or arrays of floats to a binary file, or read them from it, at any position in the file. The entire memory area in question was copied directly to the disk or read from it, with one single command, without any encoding or type switching. It was the fastest way possible, streamed directly to and from disk.
If the file did not exist, it was created automatically. If it was too short for the requested position, it was brought to the required length and filled with zeros.
Is there a library that does the same in go?

What you describe sounds almost like a database file…

Edit: What I mean is that writing and reading quickly from and to a (well, more or less) binary file is what a datatbase also provides.

If you are rather searching for a way to implement a specific binary file format, then a mmap package might be what you are looking for. (mmap = memory mapped file) (A Web search for golang mmap returns a few such packages; I have not used mmap yet so I cannot comment on any of these.)

2 Likes

Thank you.
Yes, it is some kind of a database. I have files that store some structs in the first 1024 bytes, and behind that one float32 value for each minute of the year. E.g. 1.Jan. 6:30 am is at position 2584.
I need to read an write values for a minute, a day or the whole year (~2 Mb) really fast.
Mmap seems promising, thank you.

This does sound like pascals file of <type>, which is more or less an implied marshaller that one can’t change.

In go we have to provide those marshallers explicitely for your types by implementing BinaryMarshaller and BinaryUnmarshaller from encoding-package.

Also you have to do all the file handling on your own, which involves opening, closing, seeking, etc.

3 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.