Golang : Read binary file into memory
Reading a binary file content into memory for further manipulation is easy with Go. In this tutorial, we will show how to read a binary file into buffer(memory). Go has a standard library package (http://golang.org/pkg/bufio/) to handle this kind of requirement.
The code below will just show you how to read the file content into memory. What you want to do next with the file content is up to you.
readbinaryfile.go
package main
import (
"os"
"bufio"
)
func main() {
file, err := os.Open("binary.dat")
if err != nil {
fmt.Println(err)
return
}
defer file.Close()
info, err := file.Stat()
if err != nil {
return nil, err
}
// calculate the bytes size
var size int64 = info.Size()
bytes := make([]byte, size)
// read into buffer
buffer := bufio.NewReader(file)
_,err = buffer.Read(bytes)
}
Reference :
See also : Golang : Read file with ioutil
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+3.7k Python : Find out the variable type and determine the type with simple test
+5.7k Golang : Reverse by word
+17.9k Golang : Execute shell command
+5.9k Golang : Accessing dataframe-go element by row, column and name example
+9.3k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+6.3k Javascript : Push notifications to browser with Push.js
+10.6k Golang : Gorilla web tool kit secure cookie example
+14.2k Golang : invalid character ',' looking for beginning of value
+9.3k Golang : How to delete element(data) from map ?
+4.5k Golang : ROT32768 (rotate by 0x80) UTF-8 strings example
+35.5k Upload multiple files with Go