package main import ( "bufio" "os" ) func main() { reader := bufio.NewReader(os.Stdin) line, _, err := reader.ReadLine() if err != nil { fmt.Println("Error reading input:", err) return } fmt.Println("You entered:", string(line)) }
package main import ( "bufio" "fmt" "strings" ) func main() { data := "hello\nworld\nhow are you?\n" reader := bufio.NewReader(strings.NewReader(data)) for { line, _, err := reader.ReadLine() if err != nil { break } fmt.Println("Read line:", string(line)) } }This code creates a new Reader that reads from a string rather than from an input stream. Then, it reads each line of the provided string one at a time using a for loop that runs until an error (signaling the end of the input) is returned. Each line is printed out using the fmt.Println function. In summary, the bufio package provides buffered I/O functionality for reading and writing data in Go, and the Reader type specifically allows you to read data in chunks. The ReadLine method of this type is useful for reading individual lines of input.