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
+6.5k Unix/Linux : How to get own IP address ?
+5.5k Clean up Visual Studio For Mac installation failed disk full problem
+6.5k Golang : Map within a map example
+15.2k Golang : Get HTTP protocol version example
+15.8k Golang : Get digits from integer before and after given position example
+7.4k Golang : Scanf function weird error in Windows
+7.1k Golang : Transform lisp or spinal case to Pascal case example
+19.4k Golang : How to count the number of repeated characters in a string?
+43.5k Golang : Get hardware information such as disk, memory and CPU usage
+19.1k Mac OSX : Homebrew and Golang
+11.9k Golang : Convert decimal number(integer) to IPv4 address
+11.3k Golang : Intercept and process UNIX signals example