Golang : Convert to io.ReadSeeker type
Problem :
You want to open a file, read the content into a buffer and then convert the buffer to io.ReadSeeker type.
Solution :
Create a buffer and then convert the buffer with bytes.NewReader(buffer)
function.
For example :
file, err := os.Open(fileToUpload)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer file.Close()
fileInfo, _ := file.Stat()
var size int64 = fileInfo.Size()
buffer := make([]byte, size)
// read file content to buffer
file.Read(buffer)
fileBytes := bytes.NewReader(buffer) // converted to io.ReadSeeker type
See also : Golang : Convert []byte to image
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
+42.2k Golang : Encode image to base64 example
+4.5k Golang : Detect variable or constant type
+18.4k Golang : Convert seconds to minutes and remainder seconds
+3.2k Chrome : How to block socketloop.com links in Google SERP?
+24k PHP : Count number of JSON items/objects
+14k Golang : read gzipped http response
+13.7k Golang : Get the IPv4 and IPv6 addresses for a specific network interface
+4.3k Unix/Linux : Get reboot history or check when was the last reboot date
+5.3k Golang : Get Alexa ranking data example
+25.9k Golang : Save map/struct to JSON or XML file
+11k Golang : Get dimension(width and height) of image file