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
+10.1k Fix ERROR 1045 (28000): Access denied for user 'root'@'ip-address' (using password: YES)
+13.2k Golang : How to get year, month and day?
+26.5k Golang : Force your program to run with root permissions
+8.8k Golang : Intercept and compare HTTP response code example
+17.5k Golang : How to make a file read only and set it to writable again?
+7.3k SSL : How to check if current certificate is sha1 or sha2 from command line
+7.4k Golang : Mapping Iban to Dunging alphabets
+13.5k Golang : How to determine if a year is leap year?
+9.4k Golang : Find correlation coefficient example
+7.5k Golang : get the current working directory of a running program
+27.3k PHP : Count number of JSON items/objects
+13.8k Golang : Reverse IP address for reverse DNS lookup example