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
+9.7k Swift : Convert (cast) String to Integer
+11.4k Golang : Get month name from date example
+14.3k Golang : Get timezone offset from date or timestamp
+5.9k Get Facebook friends working in same company
+28.5k Golang : Get time.Duration in year, month, week or day
+4.4k Golang : Pad file extension automagically
+7k Javascript : How to check a browser's Do Not Track status?
+11.5k Golang : Add ASCII art to command line application launching process
+31.6k Golang : Regular Expression for alphanumeric and underscore
+29.5k Golang : How to redirect to new page with net/http?
+4.8k PHP : Convert string to timestamp or datestamp before storing to database(MariaDB/MySQL)
+11.7k Golang : Exit, terminating or aborting a program