To write a string to a bufio.Writer
in Go, you can use the WriteString()
method. Here's an example:
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
// Create a new bufio.Writer
writer := bufio.NewWriter(os.Stdout)
// Write a string to the writer
str := "Hello, World!"
_, err := writer.WriteString(str)
if err != nil {
fmt.Println("Error writing string:", err)
return
}
// Flush the writer to ensure the string is written to the underlying io.Writer
err = writer.Flush()
if err != nil {
fmt.Println("Error flushing writer:", err)
return
}
}
In this example, we create a new bufio.Writer
that writes to the standard output (os.Stdout
). We then use the WriteString()
method to write the string Hello, World!
to the writer. The Flush()
function is then called to ensure that the string is written to the underlying os.Stdout
.