package main import ( "bufio" "fmt" "os" ) func main() { file, err := os.Create("test.txt") if err != nil { fmt.Println(err) } defer file.Close() writer := bufio.NewWriter(file) str := "Hello, World!" writer.WriteString(str) writer.Flush() }
package main import ( "bufio" "bytes" "fmt" ) func main() { data := []byte("Hello, World!") buffer := bytes.NewBuffer(data) readerWriter := bufio.NewReadWriter(bufio.NewReader(buffer), bufio.NewWriter(buffer)) writerString := "Gopher Land" readerWriter.WriteString(writerString) readerWriter.Flush() // flush operation is needed to write changes back to buffer result := buffer.Bytes() fmt.Println(string(result)) }In this example, we create a buffer and a new ReadWriter using that buffer. We then write a string to the writer using the WriteString function, and finally, we flush the writer to ensure that all of the data has been written to the buffer. The bufio package is used for I/O operations and is frequently used to improve the performance of I/O operations in Go. The functions discussed above are just some of the functions provided by the package.