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
+8.4k Golang : Implementing class(object-oriented programming style)
+9.1k Golang : Build and compile multiple source files
+7.9k Golang : Regular Expression find string example
+9.4k Golang : Generate EAN barcode
+15.1k Golang : package is not in GOROOT during compilation
+15.9k Golang : Get current time from the Internet time server(ntp) example
+18.8k Golang : convert int to string
+17.1k Golang : Fix cannot convert buffer (type *bytes.Buffer) to type string error
+6.8k Golang : Find the longest line of text example
+19.4k Golang : How to count the number of repeated characters in a string?
+13.7k Golang : Qt progress dialog example
+7.4k Golang : Fixing Gorilla mux http.FileServer() 404 problem