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
+24.1k Golang : Fix type interface{} has no field or no methods and type assertions example
+20.6k Swift : Convert (cast) Int to int32 or Uint32
+12.5k Golang : Extract part of string with regular expression
+25.5k Golang : Convert long hexadecimal with strconv.ParseUint example
+8.7k Golang : Convert(cast) []byte to io.Reader type
+12k Golang : GTK Input dialog box examples
+4.7k Javascript : Detect when console is activated and do something about it
+25.8k Golang : How to write CSV data to file
+5.9k Golang : ROT32768 (rotate by 0x80) UTF-8 strings example
+18.9k Unmarshal/Load CSV record into struct in Go
+5.9k Golang : Launching your executable inside a console under Linux
+8k Golang : Lock executable to a specific machine with unique hash of the machine