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.3k Golang : Reverse text lines or flip line order example
+5.2k Swift : Convert (cast) Float to Int or Int32 value
+16.5k Golang : Send email and SMTP configuration example
+6.8k Golang : Reverse by word
+10.1k Golang : Setting variable value with ldflags
+43.5k Golang : Convert []byte to image
+42.1k Golang : How do I convert int to uint8?
+22.8k Golang : Round float to precision example
+17.7k Convert JSON to CSV in Golang
+11.8k Golang : How to detect a server/machine network interface capabilities?
+10.2k Golang : Edge detection with Sobel method
+11.6k Use systeminfo to find out installed Windows Hotfix(s) or updates