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
+37.7k Golang : Convert to io.ReadSeeker type
+8.3k Golang : Web(Javascript) to server-side websocket example
+11.6k Golang : How to calculate the distance between two coordinates using Haversine formula
+5.5k Unix/Linux : Use netstat to find out IP addresses served by your website server
+35.1k Golang : Display float in 2 decimal points and rounding up or down
+34k Golang : Get file last modified date and time
+12.6k Golang : unknown escape sequence error
+12.9k Golang : concatenate(combine) strings
+26.1k PHP : Count number of JSON items/objects
+10.9k Golang : Print UTF-8 fonts on image example
+26.6k Golang : Decode/unmarshal unknown JSON data type with map[string]interface
+15.3k Golang : Convert slice to array