Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a binary file in Go

Tags:

file-io

go

I'm completely new to Go and I'm trying to read a binary file, either byte by byte or several bytes at a time. The documentation doesn't help much and I cannot find any tutorial or simple example (by the way, how could Google give their language such an un-googlable name?). Basically, how can I open a file, then read some bytes into a buffer? Any suggestion?

like image 815
laurent Avatar asked Jan 25 '13 02:01

laurent


People also ask

How do I read a binary file in Golang?

The simplest way of reading a text or binary file in Go is to use the ReadFile() function from the os package. This function reads the entire content of the file into a byte slice, so you should be careful when trying to read a large file - in this case, you should read the file line by line or in chunks.

How do I read an entire file in Golang?

To read an entire file into a variable, use the ReadFile function from the ioutil library. This is the most basic type of file reading that can be required.

How do I read a string from a file in Golang?

Inside the io/ioutil package, there's a function called ReadFile() which is used to open a file and then convert its contents into a slice of bytes, and if for some reason, it isn't able to do so, then it will return an error too. Here is the syntax of the ReadLine() function.


2 Answers

This is what I use to read an entire binary file into memory

func RetrieveROM(filename string) ([]byte, error) {     file, err := os.Open(filename)      if err != nil {         return nil, err     }     defer file.Close()      stats, statsErr := file.Stat()     if statsErr != nil {         return nil, statsErr     }      var size int64 = stats.Size()     bytes := make([]byte, size)      bufr := bufio.NewReader(file)     _,err = bufr.Read(bytes)      return bytes, err } 
like image 28
djhworld Avatar answered Oct 12 '22 00:10

djhworld


For manipulating files, the os package is your friend:

f, err := os.Open("myfile") if err != nil {    panic(err) } defer f.Close() 

For more control over how the file is open, see os.OpenFile() instead (doc).

For reading files, there are many ways. The os.File type returned by os.Open (the f in the above example) implements the io.Reader interface (it has a Read() method with the right signature), it can be used directly to read some data in a buffer (a []byte) or it can also be wrapped in a buffered reader (type bufio.Reader).

Specifically for binary data, the encoding/binary package can be useful, to read a sequence of bytes into some typed structure of data. You can see an example in the Go doc here. The binary.Read() function can be used with the file read using the os.Open() function, since as I mentioned, it is a io.Reader.

And there's also the simple to use io/ioutil package, that allows you to read the whole file at once in a byte slice (ioutil.ReadFile(), which takes a file name, so you don't even have to open/close the file yourself), or ioutil.ReadAll() which takes a io.Reader and returns a slice of bytes containing the whole file. Here's the doc on ioutil.

Finally, as others mentioned, you can google about the Go language using "golang" and you should find all you need. The golang-nuts mailing list is also a great place to look for answers (make sure to search first before posting, a lot of stuff has already been answered). To look for third-party packages, check the godoc.org website.

HTH

like image 107
mna Avatar answered Oct 12 '22 01:10

mna