There's this: https://github.com/skelterjohn/exp/blob/master/iochan/iochan.goJust something I was fooling around with a while back. Let's you do this: https://github.com/skelterjohn/exp/blob/master/iochan/iochan_test.go
That will work fine as long as you're certain no line will ever be
more than 4096 bytes(default bufio buffer size).
Ignoring the prefix return value isn't a good idea. Returning an error
value stating that the file was in an incorrect format is a good idea,
or at least panic if your assumptions are incorrect.
--
=====================
http://jessta.id.au
i usually use bufio.Reader.ReadString('\n'),
and strip off any trailing \r\n or \n as necessary.
i do think the current design of ReadLine is unfortunate - the
most common case (reading a line regardless of how
long it is, with \r\n or \n stripped) is satisfied by none
of the existing calls.
as ever, the design is complicated by the fact that the
final line may or may not be newline-terminated, and
we'd like to be able to preserve that information.
but in my experience most callers don't care about this.
so i'd be happy if the existing ReadLine was renamed to ReadLineSlice
and there were two additional calls:
// ReadLine reads a line and returns it as a string,
// not including the end-of-line bytes.
// If you need to know whether the final line is
// terminated, use ReadLineSlice instead.
func (r *Reader) ReadLine() (string, error)
// ReadLineBytes reads a line and returns it as a []byte,
// not including the end-of-line bytes.
// If you need to know whether the final line is
// terminated, use ReadLineSlice instead.
func (r *Reader) ReadLineBytes() ([]byte, error)
so the canonical "read lines" loop can look like this:
for {
line, err := r.ReadLine()
if err != nil {
break
}
doSomething(line)
}
which is a lot closer to ideal IMHO.
it's not going to happen for Go-1 though.
i think that's an optimisation that's unnecessary in most cases.
if you find that you're allocating too much in a particular
program, it's simple to write a function that does this.
func readLine(r *bufio.Reader) ([]byte, error) {
line, isPrefix, err := r.ReadLine()
if !isPrefix {
return line, err
}
buf := append([]byte(nil), line...)
for isPrefix && err == nil {
line, isPrefix, err = r.ReadLine()
buf = append(buf, line...)
}
return buf, err
}
although to be fair it's not possible to give this the same
semantics as my proposed ReadLine as there's no
way AFAICS to avoid the possibility of returning a line
and an error at the same time.