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.5k Golang : Example of how to detect which type of script a word belongs to
+15.2k Golang : Check if a string contains multiple sub-strings in []string?
+6k Swift : substringWithRange() function example
+22.2k Golang : Fix type interface{} has no field or no methods and type assertions example
+19.7k Golang : Create and resolve(read) symbolic links
+5.8k Golang : Output or print out JSON stream/encoded data
+6.5k Golang : Handling Yes No Quit query input
+12k Golang : Handle or parse date string with Z suffix(RFC3339) example
+9k Golang : Print how to use flag for your application example
+19.3k Golang : Saving private and public key to files
+8k Golang : Find the length of big.Int variable example
+5.3k Golang : Build new URL for named or registered route with Gorilla webtoolkit example