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 Golang : What fmt.Println() can do and println() cannot do
+34.2k Golang : Validate IP address
+18k Mac OSX : Homebrew and Golang
+4.5k Golang : Stop goroutine without channel
+13.7k Golang : Get all local users and print out their home directory, description and group id
+19.3k PHP : Convert(cast) int to double/float
+7.9k Golang : Apply Histogram Equalization to color images
+14.2k Golang : Delete certain files in a directory
+8.2k Random number generation with crypto/rand in Go
+12.5k Golang : Compress and decompress file with compress/flate example
+9.5k Golang : Roll the dice example
+2.6k Java : Get FX sentiment from website example