Golang : Reset or rewind io.Reader or io.Writer
Problem :
You have read file with io.Reader till EOF. Or you want to rewrite a file with io.Writer again.
How to reset or rewind the io.Reader or io.Writer?
Solution :
Looks like you are reading/accessing or writing in the file in sequential mode instead of random access mode.
Use os.File.Seek()
function to reset. Think of... like you are rewind a video or cassette tape with your finger or pencil.
For example :
imgfile, err := os.Open("./img.jpg")
if err != nil {
fmt.Println("img.jpg file not found!")
os.Exit(1)
}
defer imgfile.Close()
....
read file
....
imgfile.Seek(0, 0) // reset/rewind back to offset and whence 0 0
....
read file again
References :
http://golang.org/pkg/io/#Writer
See also : Golang : io.Reader causing panic: runtime error: invalid memory address or nil pointer dereference
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
+11.1k Golang : Create S3 bucket with official aws-sdk-go package
+7.4k Golang : Calculate how many weeks left to go in a given year
+13.2k Golang : Convert(cast) uintptr to string example
+12.8k Golang : zlib compress file example
+6.3k Golang : Get Hokkien(福建话)/Min-nan(閩南語) Pronounciations
+12.2k Golang : Save webcamera frames to video file
+12.9k Golang : Add ASCII art to command line application launching process
+46.6k Golang : Encode image to base64 example
+12.4k Golang : 2 dimensional array example
+17.2k Golang : Capture stdout of a child process and act according to the result
+10.7k Fix ERROR 1045 (28000): Access denied for user 'root'@'ip-address' (using password: YES)
+17.4k Golang : Find file size(disk usage) with filepath.Walk