Jump: skip bytes from stream using io.reader

What is the best way in Go to skip ahead a few bytes in a stream using io.Reader ? That is, is there a function in the standard library that takes a reader and a counter that will read and delete byte counters from the reader?

Usage example:

 func DoWithReader(r io.Reader) { SkipNBytes(r, 30); // Read and dispose 30 bytes from reader } 

I do not need to go back in the stream, so anything that can work without converting io.Reader to another type of reader would be preferable.

+6
source share
2 answers

You can use this construct:

 import "io" import "io/ioutil" io.CopyN(ioutil.Discard, yourReader, count) 

It copies the requested number of bytes to io.Writer , which discards what it reads.

If your io.Reader is io.Seeker , you might want to search the stream to skip the number of bytes you want to skip:

 import "io" import "io/ioutil" import "os" if s, ok = yourReader.(io.Seeker); ok { s.Seek(count, os.SEEK_CUR) // seek relative to current file pointer } else { io.CopyN(ioutil.Discard, yourReader, count) } 
+15
source

Yes:

http://golang.org/pkg/io/#ReadAtLeast

(It helps to read documents ...)

-3
source

Source: https://habr.com/ru/post/959141/


All Articles