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
+8.5k Golang : Terminate-stay-resident or daemonize your program?
+32.7k Golang : How to check if slice or array is empty?
+13.2k Golang : Check if a file exist or not
+10.8k Golang : Verify Linux user password again before executing a program example
+4.6k Swift : Convert string array to array example
+10.7k Golang : Display a text file line by line with line number example
+5.2k Fontello : How to load and use fonts?
+17.5k Golang : Get path name to current directory or folder
+11k Golang : Split strings into command line arguments
+7.1k Golang : How to feed or take banana with Gorilla Web Toolkit Session package