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
+8.8k Golang : Inject/embed Javascript before sending out to browser example
+21.6k Golang : Upload big file (larger than 100MB) to AWS S3 with multipart upload
+8.7k Golang : Find network service name from given port and protocol
+11.1k Golang : How to pipe input data to executing child process?
+39k Golang : How to read CSV file
+12.9k Golang : Convert(cast) uintptr to string example
+8.9k Golang : automatically figure out array length(size) with three dots
+7.2k Golang : Calculate how many weeks left to go in a given year
+46.2k Golang : Marshal and unmarshal json.RawMessage struct example
+10.3k Swift : Convert (cast) String to Integer
+8.4k Golang : Another camera capture GUI application with GTK and OpenCV
+11.1k Golang : Intercept and process UNIX signals example