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
+13.9k Golang : Force download file example
+29.2k Golang : Interpolating or substituting variables in string examples
+5.5k Golang : Find the longest line of text example
+21.7k Golang : Gorilla mux routing example
+5.7k Restart Apache or Nginx web server without password prompt
+5.3k Golang : Handling image beyond OpenCV video capture boundary
+7.7k nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
+11.7k Golang : http.Get example
+8.1k Golang : Play .WAV file from command line
+9.3k JavaScript/JQuery : Detect or intercept enter key pressed example
+8.4k Golang : Copy map(hash table) example
+44.3k Golang : Marshal and unmarshal json.RawMessage struct example