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
+29.2k Golang : How to redirect to new page with net/http?
+3.8k JavaScript : Rounding number to decimal formats to display currency
+12.7k Golang : convert rune to unicode hexadecimal value and back to rune character
+4k Golang : Print instead of building pyramids
+5.3k Golang : Extract sub-strings
+25.1k Golang : Calculate future date with time.Add() function
+4.8k Golang : Denco multiplexer example
+15.2k Golang : Fix cannot convert buffer (type *bytes.Buffer) to type string error
+17.2k Golang : Send email with attachment
+15.3k Golang : Send email and SMTP configuration example
+81.4k Golang : How to convert character to ASCII and back
+4.9k Golang : List all packages and search for certain package