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
+11.3k Golang : Chunk split or divide a string into smaller chunk example
+5.8k Golang : Convert(cast) io.Reader type to string
+6.3k Golang : Gonum standard normal random numbers example
+6.3k Python : Fix SyntaxError: Non-ASCII character in file, but no encoding declared
730 Golang : Switch Redis database redis.NewClient
+19.1k Golang : untar or extract tar ball archive example
+6.4k Golang : Convert word to its plural form example
+6.3k Android Studio : Import third-party library or package into Gradle Scripts
+8.4k Golang : Send data to /dev/null a.k.a blackhole with ioutil.Discard
+6.3k Golang : Check if integer is power of four example
+6.2k Golang : Randomize letters from a string example
+8.3k Golang : Concatenate (combine) buffer data example