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
+19.3k Golang : Convert PNG transparent background image to JPG or JPEG image
+21.6k Golang : Gorilla mux routing example
+5.7k Golang : Get expvar(export variables) to work with multiplexer
+10.1k CodeIgniter : How to check if a session exist in PHP?
+9k Golang : Read file and convert content to string
+8.5k Golang : interface - when and where to use examples
+9.2k Golang : How to get quoted string into another string?
+8.5k Golang : Validate IPv6 example
+4.4k Golang : If else example and common mistake
+5.5k Golang : Totalize or add-up an array or slice example
+9.6k PHP : Convert(cast) bigInt to string
+16.3k Golang : Upload/Receive file progress indicator