OS and File Operations

Go's os package provides a platform-independent interface to operating system functionality. Whether you are managing files, reading environment variables, or handling signals, the os package is your primary gateway to the host machine.


1. File I/O: Creation and Reading

Working with files in Go is straightforward but requires explicit error handling to ensure data integrity.

// Creating a file
f, err := os.Create("config.json")
if err != nil {
    log.Fatal(err)
}
defer f.Close()

// Writing data
f.WriteString("{\"active\": true}")

2. Environment Variables

Environment variables are the standard way to pass configuration to production applications (following the "12-Factor App" methodology).

// Setting and Getting
os.Setenv("APP_PORT", "8080")
port := os.Getenv("APP_PORT")

// Handling missing values
val, exists := os.LookupEnv("DB_URL")
if !exists {
    log.Fatal("DB_URL must be defined")
}

3. Command-Line Arguments

Parameters passed to your script are accessible via os.Args.

Pro-Tip: For complex command-line interfaces, use the standard library flag package or popular third-party tools like Cobra.


4. Handling Operating System Signals

In production, your application must handle "Graceful Shutdowns" when the OS sends termination signals (SIGINT, SIGTERM).

sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)

<-sigChan // Blocks until a signal is received
fmt.Println("Shutting down gracefully...")

Interview Pro-Tips: Why defer Close()?

If an interviewer asks why they see defer f.Close() everywhere:


Technical Summary

  1. os.File: Represents an open file descriptor.
  2. Stat: Use os.Stat() to check if a file exists or to read its metadata (size, modified time).
  3. Graceful Shutdown: Using signals to ensure no data is lost when the app stops.