package main import ( "bufio" "os" ) func main() { // Open file for writing file, err := os.Create("test.txt") if err != nil { panic(err) } defer file.Close() // Create a new writer with a buffer of size 10 writer := bufio.NewWriterSize(file, 10) // Write messages to the file with the writer writer.WriteString("Hello") writer.WriteString(" world") writer.Flush() // Reset the writer with a new file writer writer.Reset(file) // Write new messages to the file with the new writer writer.WriteString("How") writer.WriteString(" are") writer.WriteString(" you?") writer.Flush() }
package main import ( "bufio" "os" ) func main() { // Create a new writer with a buffer of size 10 writer := bufio.NewWriterSize(os.Stdout, 10) // Write messages to the console with the writer writer.WriteString("Hello") writer.WriteString(" world") writer.Flush() // Reset the writer with a new console writer writer.Reset(os.Stdout) // Write new messages to the console with the new writer writer.WriteString("How") writer.WriteString(" are") writer.WriteString(" you?") writer.Flush() }In this example, we are creating a new writer with a buffer of size 10 and writing messages to the console with the writer by calling WriteString() method. After writing the messages, we are resetting the writer with a new console writer and continuing to write new messages to the console with the new writer. Package Library: bufio package.