How to do it…

  1. Open the console and create the folder chapter01/recipe04.
  2. Navigate to the directory.
  1. Create the get.go file with the following content:
        package main        import (          "log"          "os"        )        func main() {          connStr := os.Getenv("DB_CONN")          log.Printf("Connection string: %s\n", connStr)        }
  1. Execute the code by calling DB_CONN=db:/user@example && go run get.go in the Terminal.
  2. See the output in the Terminal:
  1. Create the lookup.go file with the following content:
        package main        import (          "log"          "os"        )        func main() {          key := "DB_CONN"              connStr, ex := os.LookupEnv(key)          if !ex {            log.Printf("The env variable %s is not set.\n", key)          }          fmt.Println(connStr) ...

Get Go Standard Library Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.