os package
Go's standard library package for talking to the operating system: arguments, environment variables, files and the exit code.
In more detail
Its functions work the same on Linux, macOS and Windows. os.Stdout and os.Stderr are the standard output and error streams as files you can write to. Most command-line tools need little beyond os, fmt and flag.
Example
fmt.Fprintln(os.Stderr, "usage: probe <host>")Related terms
- os.Args — The slice of strings holding the words a Go program was started with, where os.Args[0] is the program itself.
- os.Getenv — The Go function that returns the value of an environment variable, or an empty string if it is not set.
- os.ReadFile — The Go function that reads a whole file into memory in one call and returns its bytes and an error.
- os.Open — The Go function that opens a file for reading and returns an *os.File you can stream from and must close.
- os.Exit — The Go function that ends the program immediately with the exit code you give it, such as 0 for success.
- standard output and standard error — The two channels a program writes normal output (stdout) and error messages (stderr) to, plus stdin for input.
Taught in
- Files and the os Package — Concurrent Systems Programming with Go