My intuitive approach: https://play.golang.org/p/6xzg7TK1IH
and it doesn't work.
Can you share some alternative ways?
My intuitive approach: https://play.golang.org/p/6xzg7TK1IH
and it doesn't work.
Can you share some alternative ways?
You use :=
which discards the previous contents of the variables. You don't use morelines
in that scope, thus the error message.
If you declare your variables beforehand and don't use :=
, it works fine. The function ReadLine()
might not do what you think it should do.
package main
import (
"bufio"
"fmt"
"log"
"os"
)
func main() {
bio := bufio.NewReader(os.Stdin)
var line []byte
var err error
moreline := true
for moreline {
line, moreline, err = bio.ReadLine()
if err != nil {
log.Fatal(err)
}
fmt.Println(line)
}
}