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
+10.1k CodeIgniter : How to check if a session exist in PHP?
+7.1k Golang : Oanda bot with Telegram and RSI example
+7.3k Android Studio : Import third-party library or package into Gradle Scripts
+10.3k Golang : Fuzzy string search or approximate string matching example
+4.6k PHP : Convert string to timestamp or datestamp before storing to database(MariaDB/MySQL)
+6.5k Golang : Handling Yes No Quit query input
+17.7k Golang : Read input from console line
+17.2k Golang : Send email with attachment
+4.4k Golang : If else example and common mistake
+4.7k Golang : Grab news article text and use NLP to get each paragraph's sentences
+10.8k Golang : Save webcamera frames to video file
+9.4k Golang : Underscore string example