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.6k Golang : Verify token from Google Authenticator App
+9.8k Golang : Find correlation coefficient example
+9.6k Golang : Get all countries currencies code in JSON format
+9.9k Golang : Qt get screen resolution and display on center example
+8.8k Golang : Set or add headers for many or different handlers
+5.5k Golang : fmt.Println prints out empty data from struct
+11k Golang : Natural string sorting example
+13.9k Golang : Qt progress dialog example
+17.8k Golang : Read data from config file and assign to variables
+10.8k Golang : Get local time and equivalent time in different time zone
+11.6k Golang : Format numbers to nearest thousands such as kilos millions billions and trillions
+13.5k Golang : Generate Code128 barcode