To read a line of text from a bufio.Reader in Go, you can use the ReadString() method. Here's an example:
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
// Create a bufio.Reader from input stream (os.Stdin)
reader := bufio.NewReader(os.Stdin)
// Read a line of text
line, err := reader.ReadString('\n')
if err != nil {
fmt.Println("Error reading line:", err)
return
}
// Print the line of text
fmt.Println("Line:", line)
}
In this example, we create a bufio.Reader from the input stream os.Stdin
(which represents the standard input). We use the ReadString() method to read a line of text, specifying the delimiter as '\n'
(newline character). The method returns the line and an error. We then check if there was any error in reading, and finally print the line of text.
You can run this program and input text from the command line, and it will print the line you entered.