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
+19k Golang : Get host name or domain name from IP address
+5.7k Golang : Generate multiplication table from an integer example
+8.4k Linux/Unix : fatal: the Postfix mail system is already running
+9.4k Javascript : Read/parse JSON data from HTTP response
+19.7k Golang : How to run your code only once with sync.Once object
+8.8k Golang : Inject/embed Javascript before sending out to browser example
+6k Golang : Calculate US Dollar Index (DXY)
+15.9k Golang : Loop each day of the current month example
+15.7k Golang : Read a file line by line
+17k Golang : How to tell if a file is compressed either gzip or zip ?
+18.2k Golang : Read binary file into memory
+4.5k Adding Skype actions such as call and chat into web page examples