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
+9.4k Golang : Copy map(hash table) example
+6.7k Golang : Fibonacci number generator examples
+13.6k Golang : Compress and decompress file with compress/flate example
+9.9k Golang : How to check if a website is served via HTTPS
+17.3k How to enable MariaDB/MySQL logs ?
+22.4k Golang : untar or extract tar ball archive example
+5.9k Linux/Unix : Commands that you need to be careful about
+9.2k Golang : Convert(cast) string to int64
+9.5k Golang : Resumable upload to Google Drive(RESTful) example
+33.6k Golang : Proper way to set function argument default value
+11.8k Golang : Pagination with go-paginator configuration example
+13.2k Golang : Qt progress dialog example