O'Reilly logo

Go Standard Library Cookbook by Radomir Sohlich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required