Provides a platform-independent interface to OS functionality: file operations, environment variables, process control, and command-line arguments. The Go equivalent of C's unistd.h and stdlib.h combined.
Import: import "os" | Docs: pkg.go.dev/os
| Name | Signature / Value | Description |
|---|---|---|
| os.Open | func Open(name string) (*File, error) | Open file for reading |
| os.Create | func Create(name string) (*File, error) | Create or truncate file for writing |
| os.OpenFile | func OpenFile(name string, flag int, perm FileMode) (*File, error) | Open with flags and permissions |
| os.ReadFile | func ReadFile(name string) ([]byte, error) | Read entire file into []byte (Go 1.16+) |
| os.WriteFile | func WriteFile(name string, data []byte, perm FileMode) error | Write []byte to file |
| os.Remove | func Remove(name string) error | Delete a file or empty directory |
| os.RemoveAll | func RemoveAll(path string) error | Delete path and all children |
| os.Rename | func Rename(oldpath, newpath string) error | Rename / move a file |
| os.MkdirAll | func MkdirAll(path string, perm FileMode) error | Create directory tree |
| os.Mkdir | func Mkdir(name string, perm FileMode) error | Create single directory |
| os.Stat | func Stat(name string) (FileInfo, error) | Return file metadata |
| os.Getenv | func Getenv(key string) string | Get environment variable value |
| os.Setenv | func Setenv(key, value string) error | Set environment variable |
| os.Args | var Args []string | Command-line arguments (Args[0] = program name) |
| os.Exit | func Exit(code int) | Terminate with exit code |
| os.Getwd | func Getwd() (dir string, err error) | Return current working directory |
| os.Chdir | func Chdir(dir string) error | Change current working directory |
| os.Stdin | *os.File | Standard input file descriptor |
| os.Stdout | *os.File | Standard output file descriptor |
| os.Stderr | *os.File | Standard error file descriptor |
| os.O_RDONLY | const = 0 | Open flag: read only |
| os.O_WRONLY | const = 1 | Open flag: write only |
| os.O_RDWR | const = 2 | Open flag: read and write |
| os.O_CREATE | const | Open flag: create if not exists |
| os.O_APPEND | const | Open flag: append to file |
| os.O_TRUNC | const | Open flag: truncate on open |
Save as main.go inside any directory and run with go run main.go. No extra packages to install — all standard library.
package main
import (
"fmt"
"os"
)
func main() {
// Command-line args
fmt.Println("Program:", os.Args[0])
if len(os.Args) > 1 {
fmt.Println("Args:", os.Args[1:])
}
// Environment variable
home := os.Getenv("HOME")
fmt.Println("HOME:", home)
// Write a file
err := os.WriteFile("hello.txt", []byte("Hello from Go!\n"), 0644)
if err != nil {
fmt.Fprintln(os.Stderr, "Write error:", err)
os.Exit(1)
}
// Read it back
data, err := os.ReadFile("hello.txt")
if err != nil {
fmt.Fprintln(os.Stderr, "Read error:", err)
os.Exit(1)
}
fmt.Print("File contents: ", string(data))
// File metadata
info, _ := os.Stat("hello.txt")
fmt.Printf("File size: %d bytes\n", info.Size())
// Cleanup
os.Remove("hello.txt")
fmt.Println("Done.")
}
// go run main.go