The search at the beginning of the file is easiest to do with File.Seek(0, 0) (or more safely with the constant: File.Seek(0, io.SeekStart) ), as you suggested, but do not forget that:
Seek behavior for a file opened with O_APPEND is not specified.
(This does not apply to your example.)
Setting a pointer to the beginning of a file is always much faster than closing and reopening a file. If you need to alternate between different “small” parts of a file multiple times, then it may be useful to open the file twice to avoid searching again (worry about this only if you have performance problems).
And again, *os.File implements io.Reader , so you can use it as io.Reader . I don’t know what ioutil.NewReader(data) you mentioned in your question (the io/ioutil does not have such a function; perhaps you meant bufio.NewReader() ?), But, of course, you do not need to read it from the file .,
source share