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
+5k Golang : Experimenting with the Rejang script
+16.2k Golang : Linked list example
+23.1k Golang : Time slice or date sort and reverse sort example
+13.3k Golang : Parsing or breaking down URL
+26.5k Golang : Decode/unmarshal unknown JSON data type with map[string]interface
+16.4k Golang : How to log each HTTP request to your web server?
+16.6k Golang : Get all upper case or lower case characters from string example
+15.6k Golang : Capture stdout of a child process and act according to the result
+4.8k Cash Flow : 50 days to pay your credit card debt
+11.7k Python : Convert IPv6 address to decimal and back to IPv6
+21.1k Golang : Read a file into an array or slice example
+16.1k Golang : Iterate linked list example