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?
-
1It helps to use the term `golang` to search google. – Daniel Jan 25 '13 at 02:07
-
1http://stackoverflow.com/questions/1821811/how-to-read-write-from-to-file – Vahid Farahmand Jan 25 '13 at 02:07
-
1You might find this link useful. http://stackoverflow.com/questions/1821811/how-to-read-write-from-to-file – Daniel Jan 25 '13 at 02:11
5 Answers
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

- 22,989
- 6
- 46
- 49
-
As of Go 1.16, `ioutil.ReadFile` is [deprecated](https://pkg.go.dev/io/ioutil#ReadFile). Use `os.ReadFile`. – Dmitrii I. Sep 12 '22 at 12:21
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
}

- 6,726
- 4
- 30
- 44
-
8Why don't you simply use `func ReadFile(filename string) ([]byte, error)` from the Go `io/ioutil` package? – peterSO Jan 28 '13 at 23:04
-
4@djhworld funny, I'm writing an emulator in Go at the moment and I stumbled across your answer after googling about binary files ;) – Rob Morgan Jul 26 '18 at 15:28
-
I recommend not using `bytes` as name for the `[]byte` because this is a library's name as well, maybe putting some other name will be better idea, like `byteArray` or something like that, other than that, this works fine. – Cesar Celis Jul 26 '22 at 16:22
For example, to count the number of zero bytes in a file:
package main
import (
"fmt"
"io"
"os"
)
func main() {
f, err := os.Open("filename")
if err != nil {
fmt.Println(err)
return
}
defer f.Close()
data := make([]byte, 4096)
zeroes := 0
for {
data = data[:cap(data)]
n, err := f.Read(data)
if err != nil {
if err == io.EOF {
break
}
fmt.Println(err)
return
}
data = data[:n]
for _, b := range data {
if b == 0 {
zeroes++
}
}
}
fmt.Println("zeroes:", zeroes)
}

- 158,998
- 31
- 281
- 276
You can't whimsically cast primitive types to (char*) like in C, so for any sort of (de)serializing of binary data use the encoding/binary package. http://golang.org/pkg/encoding/binary . I can't improve on the examples there.

- 101
- 5
Here is an example using Read
method:
package main
import (
"io"
"os"
)
func main() {
f, e := os.Open("a.go")
if e != nil {
panic(e)
}
defer f.Close()
for {
b := make([]byte, 10)
_, e = f.Read(b)
if e == io.EOF {
break
} else if e != nil {
panic(e)
}
// do something here
}
}

- 1
- 62
- 391
- 407