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
+14.4k Golang : Send email with attachment(RFC2822) using Gmail API example
+12k Golang : Pagination with go-paginator configuration example
+10.7k Golang : Get UDP client IP address and differentiate clients by port number
+22.3k Golang : How to read JPG(JPEG), GIF and PNG files ?
+9.1k Golang : How to check if a string with spaces in between is numeric?
+21.1k Golang : How to get time zone and load different time zone?
+29.7k Golang : How to get HTTP request header information?
+10.4k Golang : Get local time and equivalent time in different time zone
+7k Golang : Transform lisp or spinal case to Pascal case example
+11k Golang : Proper way to test CIDR membership of an IP 4 or 6 address example
+5.5k Golang : Detect words using using consecutive letters in a given string
+18.3k Golang : Get download file size