Code with Go
About
  • Code With Go! 🚀
  • Hello World
  • Values
  • Variables
  • Constants
  • For Loop
  • If Else
  • Switch
  • Array
  • Slice
  • Map
  • Range
  • Functions
  • Multiple Return Values
  • Variadic Functions
  • Closure
  • Recursion
  • Pointers
  • Strings and Runes
  • Structs
  • Methods
  • Interfaces
  • Struct Embedding
  • Generics
  • Error
  • Goroutines
  • Channels
  • Channel Buffering
  • Channel Synchronization
  • Channel Directions
  • Select
  • Timeouts
  • Non-Blocking Channel Operations
  • Closing Channels
  • Range over Channels
  • Timers
  • Tickers
  • Worker Pools
  • Wait Groups
  • Rate Limiting
  • Atomic Counters
  • Mutexes
  • Stateful Goroutines
  • Sorting
  • Sorting by Functions
  • Panic
  • Defer
  • Recover
  • String Functions
  • String Formatting
  • Text Templates
  • Regular Expressions
  • JSON
  • XML
  • Time
  • Epoch
  • Time Formatting / Parsing
  • Random Numbers
  • Number Parsing
  • SHA256 Hashes
  • Base64 Encoding
  • Reading Files
  • Writing Files
  • Line Filters
  • File Paths
  • Directories
  • Temporary Files and Directories
  • Embed Directive
  • Testing and Benchmarking
  • Command-Line Arguments
  • Command-Line Flags
  • Command-Line Subcommands
  • Environment Variables
  • Logging
  • HTTP Client
  • HTTP Server
  • Context
  • Spawning Processes
  • Exec'ing Processes
  • Signals
  • Exit
  • Resize Image
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub

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 (
	"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

wrote 5 bytes
wrote 7 bytes
wrote 9 bytes

Explanation:

  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. 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. Flushing and Syncing:

    • f.Sync() ensures that all changes to the file are flushed to disk.

  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.

PreviousReading FilesNextLine Filters

Last updated 1 year ago

Was this helpful?