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.7k Golang : Go as a script or running go with shebang/hashbang style
+11.9k Golang : List running EC2 instances and descriptions
+10.7k Golang : Create Temporary File
+5.5k Golang : Error handling methods
+14.1k Golang : Parsing or breaking down URL
+11k Use systeminfo to find out installed Windows Hotfix(s) or updates
+32.8k Delete a directory in Go
+15.3k Golang : How to convert(cast) IP address to string?
+7.3k Golang : Shuffle strings array
+12k Golang : How to check if a string starts or ends with certain characters or words?
+4.7k Google : Block or disable caching of your website content
+5.5k Unix/Linux : Get reboot history or check when was the last reboot date