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
+34k Golang : Create x509 certificate, private and public keys
+8.5k Golang : Another camera capture GUI application with GTK and OpenCV
+10.4k Swift : Convert (cast) String to Integer
+9.4k Golang : Accessing content anonymously with Tor
+26.6k Golang : How to check if a connection to database is still alive ?
+6.3k PHP : Proper way to get UTF-8 character or string length
+4.3k Golang : Valued expressions and functions example
+5.5k Golang : Configure crontab to poll every two minutes 8am to 6pm Monday to Friday
+5.4k How to check with curl if my website or the asset is gzipped ?
+7.3k Golang : How to convert strange string to JSON with json.MarshalIndent
+18.9k Golang : When to use public and private identifier(variable) and how to make the identifier public or private?