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
+6.7k Golang : Set or add headers for many or different handlers
+10.6k Golang : Covert map/slice/array to JSON or XML format
+14.8k Golang : How to run your code only once with sync.Once object
+3.5k Golang : Check if a word is countable or not
+15.5k Golang : How to get own program name during runtime ?
+14.3k Golang :Trim white spaces from a string
+13.9k Golang : Check if a string contains multiple sub-strings in []string?
+5.2k Golang : Accessing dataframe-go element by row, column and name example
+5k Golang : How to detect if a sentence ends with a punctuation?
+9.2k Golang : Get remaining text such as id or filename after last segment in URL path
+8.7k Golang : Interfacing with PayPal's IPN(Instant Payment Notification) example
+5.4k Golang : Fibonacci number generator examples