Code with Go
Ask or search…

Writing Files

This Go code demonstrates writing to files using various methods from the os and bufio packages. Let's go through the code with inline comments and explanations:
// Importing necessary packages.
import (
// Function to check for errors and panic if an error is encountered.
func check(e error) {
if e != nil {
// 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)
// Creating a new file and writing bytes to it using os.Create.
f, err := os.Create("/tmp/dat2")
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)
fmt.Printf("wrote %d bytes\n", n2)
// Writing a string to the file using f.WriteString.
n3, err := f.WriteString("writes\n")
fmt.Printf("wrote %d bytes\n", n3)
// Ensuring that all changes to the file are flushed to disk.
// Creating a buffered writer for efficient writing.
w := bufio.NewWriter(f)
n4, err := w.WriteString("buffered\n")
fmt.Printf("wrote %d bytes\n", n4)
// Flushing the buffered writer to ensure all data is written to the file.


wrote 5 bytes
wrote 7 bytes
wrote 9 bytes
  1. 1.
    Writing to Files:
    • os.WriteFile("/tmp/dat1", d1, 0644) writes the byte slice d1 to a file named "dat1".
    • os.Create("/tmp/dat2") creates a new file or truncates an existing one.
  2. 2.
    Writing Bytes and Strings:
    • f.Write(d2) writes the byte slice d2 to the file.
    • f.WriteString("writes\n") writes the string "writes" to the file.
  3. 3.
    Flushing and Syncing:
    • f.Sync() ensures that all changes to the file are flushed to disk.
  4. 4.
    Buffered Writing:
    • bufio.NewWriter(f) creates a buffered writer for efficient writing.
    • w.WriteString("buffered\n") writes the string "buffered" using the buffered writer.
    • w.Flush() ensures that all data buffered in the writer is written to the file.
This code demonstrates different methods for writing data to files in Go, including direct writing, buffered writing, and flushing changes to disk.