package main import ( "bufio" "fmt" "os" ) func main() { file, err := os.Create("output.txt") if err != nil { fmt.Println("Error creating file:", err) return } defer file.Close() writer := bufio.NewWriter(file) // Writing to the writer writer.WriteString("This is the first line\n") writer.WriteString("This is the second line\n") // Flush buffered data to the file writer.Flush() }
package main import ( "bufio" "fmt" "net" ) func main() { conn, err := net.Dial("tcp", "localhost:8080") if err != nil { fmt.Println("Error connecting:", err) return } defer conn.Close() writer := bufio.NewWriter(conn) // Writing to the writer writer.WriteString("GET / HTTP/1.0\n\n") // Flush buffered data to the network connection writer.Flush() }In this example, we create a new connection to a server running on localhost at port 8080. We create a new Writer struct to write to the network connection. We write a HTTP GET request to the writer and flush the buffered data to the network connection using Flush method of the writer. In conclusion, the Go bufio Package provides the Writer structure and Flush method that allows for buffered writing to underlying writer, in our examples we have flushed buffered data to local file system and network connection.