package main import ( "bufio" "fmt" "os" ) func main() { file, _ := os.Create("output.txt") writer := bufio.NewWriter(file) writer.WriteString("This is an example\n") writer.Flush() file.Close() }
package main import ( "bufio" "fmt" "io/ioutil" "os" ) func main() { tempFile, _ := ioutil.TempFile("", "example") defer os.Remove(tempFile.Name()) writer := bufio.NewWriter(tempFile) writer.WriteString("This is an example\n") writer.Flush() reader := bufio.NewReader(tempFile) line, _ := reader.ReadString('\n') fmt.Println(line) tempFile.Close() }This example creates a temporary file using the ioutil package and writes a string to it using the bufio package. The Flush method ensures that any buffered data is written to the file before it is read. The ReadString method of the bufio package is used to read the first line of the file. The temp file is closed at the end of the program. Overall, the go bufio package provides functionality for buffering I/O operations in Go. The ReadWriter Flush method ensures that any buffered data is written to the underlying writer.