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
+5k Golang : Check if a word is countable or not
+50.8k Golang : Disable security check for HTTPS(SSL) with bad or expired certificate
+11.4k Golang : Handle API query by curl with Gorilla Queries example
+7.5k Golang : Convert(cast) io.Reader type to string
+7.5k Golang : Mapping Iban to Dunging alphabets
+12.4k Golang : Arithmetic operation with numerical slices or arrays example
+9.1k Golang : How to check if a string with spaces in between is numeric?
+6k Golang : Build new URL for named or registered route with Gorilla webtoolkit example
+13.4k Golang : Get user input until a command or receive a word to stop
+5.4k Golang : Display advertisement images or strings on random order
+5.2k Golang : Return multiple values from function
+17.5k Golang : Defer function inside init()