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.1k Golang : Check if directory exist and create if does not exist
+20.6k Golang : Convert PNG transparent background image to JPG or JPEG image
+13.3k Golang : Read XML elements data with xml.CharData example
+23.9k Golang : Upload to S3 with official aws-sdk-go package
+29.3k Golang : Login(Authenticate) with Facebook example
+9.7k Golang : Turn string or text file into slice example
+3.9k Detect if Google Analytics and Developer Media are loaded properly or not
+20.9k Golang : Sort and reverse sort a slice of strings
+30.9k Golang : Calculate percentage change of two values
+4.8k Nginx and PageSpeed build from source CentOS example
+8k Golang : Append and add item in slice
+20k Golang : Convert seconds to human readable time format example