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
+12.3k Golang : List running EC2 instances and descriptions
+21.2k Golang : For loop continue,break and range
+19.4k Golang : Calculate entire request body length during run time
+16.4k Golang : How to extract links from web page ?
+8.5k Golang : Generate Datamatrix barcode
+33.7k Golang : How to check if slice or array is empty?
+5.1k Golang : Display packages names during compilation
+15.3k JavaScript/JQuery : Detect or intercept enter key pressed example
+5.8k Golang : ROT32768 (rotate by 0x80) UTF-8 strings example
+36.1k Golang : Integer is between a range
+7.5k Android Studio : How to detect camera, activate and capture example
+40.2k Golang : UDP client server read write example