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 Swift : Convert (cast) Int to String ?
+7.1k Golang : Regular Expression find string example
+4.6k Python : Delay with time.sleep() function example
+5.6k Golang : How to solve "too many .rsrc sections" error?
+18.3k Golang : Get current URL example
+3.7k Java : Generate multiplication table example
+13.5k Golang : On enumeration
+11.8k Golang : Transform comma separated string to slice example
+10.4k CodeIgniter : How to check if a session exist in PHP?
+29.9k Golang : How to convert(cast) string to IP address?
+6.6k Golang : Rot13 and Rot5 algorithms example
+27.2k Golang : Read, Write(Create) and Delete Cookie example