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
+29.5k Golang : Save map/struct to JSON or XML file
+5.2k Golang : Get a list of crosses(instruments) available to trade from Oanda account
+6.4k PHP : How to handle URI or URL with non-ASCII characters such as Chinese/Japanese/Korean(CJK) ?
+19.4k Golang : Calculate entire request body length during run time
+32.9k Golang : Regular Expression for alphanumeric and underscore
+11.2k Golang : Generate random elements without repetition or duplicate
+12.4k Golang : Simple client-server HMAC authentication without SSL example
+24.7k Golang : Time slice or date sort and reverse sort example
+29.6k Golang : Login(Authenticate) with Facebook example
+41.4k Golang : How to count duplicate items in slice/array?
+5.6k Clean up Visual Studio For Mac installation failed disk full problem
+6.9k Golang : Experimental emojis or emoticons icons programming language