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
+8k Useful methods to access blocked websites
+19.4k Golang : How to get own program name during runtime ?
+25.8k Golang : Convert(cast) string to uint8 type and back to string
+8k Golang : How to check variable or object type during runtime?
+4.6k Unix/Linux : secure copying between servers with SCP command examples
+17.4k Golang : How to log each HTTP request to your web server?
+6.4k Golang : Output or print out JSON stream/encoded data
+46.9k Golang : Convert int to byte array([]byte)
+12.1k Golang : Arithmetic operation with numerical slices or arrays example
+6.5k Fix sudo yum hang problem with no output or error messages
+4.6k Golang : Calculate a pip value and distance to target profit example
+13.4k Golang : How to check if a file is hidden?