liveBook · Manning

Os package

As explained in Go in Practice, Second Edition

The Go os package provides portable functions for interacting with operating-system facilities such as environment variables, files, and processes.

The Go os package connects applications to operating-system facilities. Use it to read environment-based configuration, inspect host details, obtain process information, and select portable path separators. section 2.2.2 section 12.4.1

Common operations include:

  • Read configuration with os.Getenv; missing variables return an empty string, so critical values can be validated before startup. section 2.2.2
  • Convert string configuration with strconv.ParseInt when an environment value, such as PORT, must become an integer. section 2.2.2
  • Inspect the runtime host with os.Hostname, os.Getpid, os.Getwd, os.PathSeparator, and os.PathListSeparator. section 12.4.1
  • Combine os.Hostname with net.LookupHost to discover addresses associated with the machine’s system-known hostname. section 12.4.1

Fail fast on missing server configuration

Read PORT with os.Getenv, panic when it is empty, and pass the configured value to http.ListenAndServe. Namespace variables such as MYAPP_PORT to reduce conflicts, and treat environment contents as potentially visible to subprocesses. section 2.2.2

Listing 2.12 Environment variable-based configuration
package main

import (
    "fmt"
    "net/http"
    "os"
)

func main() {
    var port string
    if port = os.Getenv("PORT"); port == "" {              #1
    panic("environment variable PORT has not been set!")      #2
    }
    http.HandleFunc("/", homePage)
    http.ListenAndServe(":"+port, nil)      #3
}

func homePage(res http.ResponseWriter, req *http.Request) {
    if req.URL.Path != "/" {
        http.NotFound(res, req)
        return
    }
    fmt.Fprint(res, "The homepage.")
}

Runnable Go example showing environment-variable validation followed by HTTP server startup. section 2.2.2

Listing 12.10 Looking up the host’s IP addresses via the hostname
func main() {
     name, err := os.Hostname()    #1
     if err != nil {               #1
            fmt.Println(err)       #1
            return                 #1
     }                             #1
     addrs, err := net.LookupHost(name)     #2
     if err != nil {                        #2
            fmt.Println(err)                #2
            return                          #2
     }                                      #2
     for _, a := range addrs {  #3
            fmt.Println(a)      #3
     }

Runnable Go example showing hostname retrieval, error handling, address lookup, and iteration over returned addresses. section 12.4.1

How Go in Practice, Second Edition covers it

How deep it goes
Explains concrete functions and runnable patterns, including validation and package composition, but stops short of broader os facilities.
Examples
Uses PORT-based HTTP startup, hostname-to-address lookup, and executable availability checks.
What you can do after
Apply it via os functions to configure services, inspect hosts, and construct portable runtime checks.
How it connects
Connects environment variables, strconv, net, HTTP servers, hostname resolution, and process execution.

Also explained in

Compare how 3 books explain os package →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Practice, Second Edition. The section links point to the parts of the book it draws on.