Writing Files
// Importing necessary packages.
import (
"bufio"
"fmt"
"os"
)
// Function to check for errors and panic if an error is encountered.
func check(e error) {
if e != nil {
panic(e)
}
}
// The main function, where the execution of the program begins.
func main() {
// Writing a byte slice to a file using os.WriteFile.
d1 := []byte("hello\ngo\n")
err := os.WriteFile("/tmp/dat1", d1, 0644)
check(err)
// Creating a new file and writing bytes to it using os.Create.
f, err := os.Create("/tmp/dat2")
check(err)
defer f.Close() // Ensuring the file is closed when the function exits.
// Writing a byte slice to the file.
d2 := []byte{115, 111, 109, 101, 10}
n2, err := f.Write(d2)
check(err)
fmt.Printf("wrote %d bytes\n", n2)
// Writing a string to the file using f.WriteString.
n3, err := f.WriteString("writes\n")
check(err)
fmt.Printf("wrote %d bytes\n", n3)
// Ensuring that all changes to the file are flushed to disk.
f.Sync()
// Creating a buffered writer for efficient writing.
w := bufio.NewWriter(f)
n4, err := w.WriteString("buffered\n")
check(err)
fmt.Printf("wrote %d bytes\n", n4)
// Flushing the buffered writer to ensure all data is written to the file.
w.Flush()
}Output
Last updated