b := bufio.NewReader(r)
line, err := b.ReadSlice('\n')
if err != nil {
// handle it!
}
If the there's the potential for someone to have malicious input in
your input, use ReadLine instead and take isPrefix in consideration.
http://golang.org/pkg/bufio/#Reader.ReadSlice
http://golang.org/pkg/bufio/#Reader.ReadLine
To split the words have a look at strings.Split:
http://golang.org/pkg/strings#Split
--
Gustavo Niemeyer
http://niemeyer.net
http://niemeyer.net/plus
http://niemeyer.net/twitter
http://niemeyer.net/blog
-- I never filed a patent.
word1 word2 word3 \n
word4 word4 word6 \n
As a result i want for each line a []string like
sentence[0] = [word1, word2, word3]
sentence[1] = [word4, word5, word6]
I can scan each character manually and create the strings myselve, but
I feel there there should be an easier way with fmt.Fscan for example.
I do not see how to get that to work properly though.
Does anyone know how to do this nicely?
func scanUntil(r *bufio.Reader, delim byte) (tokens []string, err
os.Error) {
t := make([]byte, 0, 32)
c, err := r.ReadByte()
for err == nil && c != delim {
if isSpace(c) {
if len(t) > 0 {
tokens = append(tokens, string(t))
t = t[0:0]
}
} else {
t = append(t, c)
}
c, err = r.ReadByte()
}
return
}