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
+52.1k Golang : How to get time in milliseconds?
+11.5k Golang : Intercept and process UNIX signals example
+5.1k Python : Find out the variable type and determine the type with simple test
+23.1k Golang : Calculate time different
+9.9k Golang : Format strings to SEO friendly URL example
+12.1k Golang : How to parse plain email text and process email header?
+24.2k Golang : Find biggest/largest number in array
+9k Golang : On lambda, anonymous, inline functions and function literals
+6.1k Golang : Shuffle array of list
+5.1k Golang : micron to centimeter example
+7.7k Golang : How to stop user from directly running an executable file?
+12.3k Golang : Detect user location with HTML5 geo-location