### Installing godotenv as a Command-Line Tool (Go >= 1.17) Source: https://github.com/joho/godotenv/blob/main/README.md This command installs the godotenv executable for Go versions 1.17 and later, enabling its use from the command line to load .env files before running other commands. ```Shell go install github.com/joho/godotenv/cmd/godotenv@latest ``` -------------------------------- ### Installing godotenv as a Library Source: https://github.com/joho/godotenv/blob/main/README.md This command installs the godotenv library into your Go modules, allowing you to import and use it within your Go applications. ```Shell go get github.com/joho/godotenv ``` -------------------------------- ### Installing godotenv as a Command-Line Tool (Go < 1.17) Source: https://github.com/joho/godotenv/blob/main/README.md This command installs the godotenv executable for Go versions prior to 1.17, enabling its use from the command line to load .env files before running other commands. ```Shell go get github.com/joho/godotenv/cmd/godotenv ``` -------------------------------- ### Basic .env File Example Source: https://github.com/joho/godotenv/blob/main/README.md This snippet shows a simple .env file structure with key-value pairs. These variables will be loaded into the environment when godotenv is used. ```Shell S3_BUCKET=YOURS3BUCKET SECRET_KEY=YOURSECRETKEYGOESHERE ``` -------------------------------- ### Autoloading .env File on Import Source: https://github.com/joho/godotenv/blob/main/README.md This Go snippet shows how to use the autoload package for automatic loading of the default .env file simply by importing it. This is a convenience method for quick setup. ```Go import _ "github.com/joho/godotenv/autoload" ``` -------------------------------- ### YAML-Style .env File Example Source: https://github.com/joho/godotenv/blob/main/README.md This snippet shows an alternative YAML-like syntax that godotenv can parse for defining environment variables in the .env file. ```YAML FOO: bar BAR: baz ``` -------------------------------- ### Loading Specific .env Files Source: https://github.com/joho/godotenv/blob/main/README.md This Go code demonstrates how to explicitly specify one or more .env file paths to load instead of relying on the default location. Files are loaded in the order provided. ```Go godotenv.Load("somerandomfile") godotenv.Load("filenumberone.env", "filenumbertwo.env") ``` -------------------------------- ### Loading .env File in Go Application Source: https://github.com/joho/godotenv/blob/main/README.md This Go code demonstrates the basic usage of godotenv.Load() to load variables from the default .env file in the project root. It includes error handling and accessing loaded variables using os.Getenv. ```Go package main import ( "log" "os" "github.com/joho/godotenv" ) func main() { err := godotenv.Load() if err != nil { log.Fatal("Error loading .env file") } s3Bucket := os.Getenv("S3_BUCKET") secretKey := os.Getenv("SECRET_KEY") // now do something with s3 or whatever } ``` -------------------------------- ### Loading Multiple .env Files with Precedence Source: https://github.com/joho/godotenv/blob/main/README.md This Go code illustrates the recommended convention for loading multiple .env files based on an environment variable (e.g., FOO_ENV). It loads files in a specific order (.env.{env}.local, .env.local, .env.{env}, .env) respecting existing environment variables. ```Go env := os.Getenv("FOO_ENV") if "" == env { env = "development" } godotenv.Load(".env." + env + ".local") if "test" != env { godotenv.Load(".env.local") } godotenv.Load(".env." + env) godotenv.Load() // The Original .env ``` -------------------------------- ### Using godotenv in Command Mode Source: https://github.com/joho/godotenv/blob/main/README.md This shell command demonstrates how to use the godotenv executable to load environment variables from a specified .env file before executing another command. The loaded variables are available to the executed command. ```Shell godotenv -f /some/path/to/.env some_command with some args ``` -------------------------------- ### Advanced .env File with Comments and Export Source: https://github.com/joho/godotenv/blob/main/README.md This snippet illustrates advanced features supported in .env files, including full-line comments, end-of-line comments, and the 'export' keyword. ```Shell # I am a comment and that is OK SOME_VAR=someval FOO=BAR # comments at line end are OK too export BAR=BAZ ``` -------------------------------- ### Writing Environment Map to a File Source: https://github.com/joho/godotenv/blob/main/README.md This Go code shows how to use godotenv.Write() to take a map of environment variables and write them to a file in the correct .env format, including proper escaping. ```Go env, err := godotenv.Unmarshal("KEY=value") err := godotenv.Write(env, "./.env") ``` -------------------------------- ### Marshaling Environment Map to a String Source: https://github.com/joho/godotenv/blob/main/README.md This Go snippet demonstrates using godotenv.Marshal() to convert a map of environment variables into a single string formatted as a .env file. This is useful for generating .env content programmatically. ```Go env, err := godotenv.Unmarshal("KEY=value") content, err := godotenv.Marshal(env) ``` -------------------------------- ### Reading .env into a Map Source: https://github.com/joho/godotenv/blob/main/README.md This Go code demonstrates using godotenv.Read() to parse the default .env file and return the variables as a map instead of loading them into the process environment. This is useful if you don't want to modify the global environment. ```Go var myEnv map[string]string myEnv, err := godotenv.Read() s3Bucket := myEnv["S3_BUCKET"] ``` -------------------------------- ### Parsing .env from an io.Reader Source: https://github.com/joho/godotenv/blob/main/README.md This Go snippet shows how to use godotenv.Parse() to read and parse environment variables from any source that implements the io.Reader interface, such as a network stream or an in-memory buffer. ```Go reader := getRemoteFile() myEnv, err := godotenv.Parse(reader) ``` -------------------------------- ### Unmarshalling .env from a String Source: https://github.com/joho/godotenv/blob/main/README.md This Go code demonstrates using godotenv.Unmarshal() to parse environment variables directly from a string containing the .env file content. This is useful for parsing configuration from non-file sources. ```Go content := getRemoteFileContent() myEnv, err := godotenv.Unmarshal(content) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.