Hone logo
Hone
Problems

Configuration with Environment Variables in Go

Environment variables are a crucial aspect of configuring applications, especially in deployment scenarios. This challenge asks you to build a Go program that reads configuration values from environment variables, providing a flexible and portable way to manage application settings without modifying the code itself. This is essential for deploying applications across different environments (development, staging, production) with varying configurations.

Problem Description

You need to create a Go program that reads three configuration values from environment variables: API_KEY, PORT, and DEBUG.

  • API_KEY: A string representing the API key. If the environment variable is not set, default to "default_api_key".
  • PORT: An integer representing the port number the application should listen on. If the environment variable is not set, default to 8080.
  • DEBUG: A boolean representing whether the application should run in debug mode. If the environment variable is not set, default to false. The environment variable should be a string ("true" or "false", case-insensitive).

The program should then print the retrieved configuration values to the console in a clear and readable format. Error handling for the PORT conversion to an integer is required.

Examples

Example 1:

Input:
API_KEY=my_secret_key
PORT=9000
DEBUG=true

Output:

API Key: my_secret_key
Port: 9000
Debug: true

Explanation: The program successfully reads the environment variables and prints their values.

Example 2:

Input:
PORT=abc

Output:

API Key: default_api_key
Port: 8080
Debug: false
Error: could not convert PORT to integer: abc

Explanation: The program reads API_KEY and DEBUG with their default values. It attempts to convert PORT to an integer but fails, printing an error message and using the default port.

Example 3:

Input: (No environment variables set)

Output:

API Key: default_api_key
Port: 8080
Debug: false

Explanation: All environment variables are missing, so the program uses the default values for all configurations.

Constraints

  • The program must handle cases where environment variables are not set.
  • The PORT environment variable must be converted to an integer. The program must gracefully handle invalid integer values (e.g., non-numeric strings) by printing an error message and using the default value.
  • The DEBUG environment variable must be converted to a boolean. The program should treat "true" (case-insensitive) as true and anything else as false.
  • The program should be concise and readable.
  • The program should not panic.

Notes

  • Use the os package to access environment variables.
  • Consider using strconv.Atoi to convert the PORT string to an integer.
  • Think about how to handle case-insensitive comparisons for the DEBUG variable.
  • Error handling is important for robustness. Don't just assume environment variables will always be present or in the correct format.
  • Focus on clear and informative error messages.
Loading editor...
go