- Open the console and create the folder chapter01/recipe04.
- Navigate to the directory.
- 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) }
- Execute the code by calling DB_CONN=db:/user@example && go run get.go in the Terminal.
- See the output in the Terminal:
- 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) ...