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
+13.2k Golang : Read XML elements data with xml.CharData example
+3.1k Golang : Fix go-cron set time not working issue
+7k Golang : Example of custom handler for Gorilla's Path usage.
+10.3k Golang : Get local time and equivalent time in different time zone
+35.8k Golang : How to split or chunking a file to smaller pieces?
+6.5k Golang : Humanize and Titleize functions
+25k Golang : Daemonizing a simple web server process example
+9k Golang : Temperatures conversion example
+11.2k Golang : Find age or leap age from date of birth example
+28.9k Golang : Record voice(audio) from microphone to .WAV file
+5.9k Golang : Get missing location after unmarshal binary and gob decode time.
+26.4k Golang : How to check if a connection to database is still alive ?