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
+13.2k Golang : GUI with Qt and OpenCV to capture image from camera
+20k Golang : GORM create record or insert new record into database example
+4k MariaDB/MySQL : Form select statement or search query with Chinese characters
+26.7k Golang : Change a file last modified date and time
+4.5k Javascript : How to loop over and parse JSON data?
+8.3k Random number generation with crypto/rand in Go
+13.8k Golang : Get HTTP protocol version example
+4.5k Golang : Detect words using using consecutive letters in a given string
+7.7k Linux/Unix : fatal: the Postfix mail system is already running
+4.9k Unix/Linux : How to test user agents blocked successfully ?
+13.1k Golang : How to determine if user agent is a mobile device example
+11.8k Golang : Convert(cast) uintptr to string example