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
+31.3k Golang : Regular Expression for alphanumeric and underscore
+18.5k Golang : Check if os.Stdin input data is piped or from terminal
+8k Golang : Write multiple lines or divide string into multiple lines
+6.6k SSL : How to check if current certificate is sha1 or sha2 from command line
+11.7k Golang : Drop cookie to visitor's browser and http.SetCookie() example
+8.2k Golang : Play .WAV file from command line
+22.9k Golang : How to validate URL the right way
+8.5k Golang : How to generate Code 39 barcode?
+50.6k Golang : How to get time in milliseconds?
+4.9k Cash Flow : 50 days to pay your credit card debt
+3.9k Linux : How to set root password in Linux Mint
+8k Golang : How to extract video or image files from html source code