env v1

The env package provides a simple way to manage environment variables in Go applications. It allows you to retrieve environment variables as different data types and handle required variables graceful

Repository

Installation

Creating an Environment Variable

To create a new environment variable, use the New function. You can specify whether the variable is required. If a required variable is not set, the program will panic.

Methods

String()

Returns the environment variable value as a string.

Strings()

Returns the environment variable value as a string.

Int()

Returns the environment variable value as an integer. Returns 0 if the conversion fails.

Bool()

Returns the environment variable value as a boolean. Returns false if the conversion fails.

Float()

Returns the environment variable value as a float. Returns 0.0 if the conversion fails.

Uint()

Returns the environment variable value as an unsigned integer. Returns 0 if the conversion fails.

Example

Here's a complete example demonstrating how to use the env package:

This example sets an environment variable MY_ENV_VAR and demonstrates how to retrieve its value in various formats using the env package.

Last updated