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
+6.4k Golang : Not able to grep log.Println() output
+37.8k Golang : How to iterate over a []string(array)
+50.1k Golang : Check if item is in slice/array
+22.8k Golang : Daemonizing a simple web server process example
+11.9k Golang : List objects in AWS S3 bucket
+7.7k Golang : Sort lines of text example
+10.4k SSL : The certificate is not trusted because no issuer chain was provided
+16.4k How to enable MariaDB/MySQL logs ?
+4.5k Clean up Visual Studio For Mac installation failed disk full problem
+6.3k Golang : Getting Echo framework StartAutoTLS to work
+18.3k Golang : Close channel after ticker stopped example
+20.5k Golang : Join arrays or slices example