package main import ( "bufio" "fmt" "strings" ) func main() { message := "Hello, world!" // create a new ReadWriter rw := bufio.NewReadWriter( bufio.NewReader(strings.NewReader(message)), bufio.NewWriter(nil), ) // read the message from the buffer and print it received, _ := rw.ReadString('!') fmt.Printf("Received message: %s\n", received) }
package main import ( "bufio" "fmt" "os" ) func main() { // create a new ReadWriter using standard input/output rw := bufio.NewReadWriter( bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout), ) // read a line from the user and echo it back line, _ := rw.ReadString('\n') rw.WriteString(fmt.Sprintf("You said: %s", line)) rw.Flush() }In this example, we create a new `ReadWriter` using standard input and output as the input and output sources respectively. We then use the `ReadString` method to read a line of text from the user, and write it back to the console using the `WriteString` method. Finally, we flush the buffer using the `Flush` method to ensure that all data is written to the output stream. Overall, the `bufio` package library in Go provides versatile buffering functionality that can improve the performance of I/O operations in a variety of situations.