Sobes.tech
Middle+

What is a .env file and what is it used for?

sobes.tech AI

Answer from AI

.env file is a text file that stores environment variables for an application. It usually contains confidential or configurable parameters such as database connection strings, API keys, port settings, etc.

Using a .env file allows:

  • Separating configuration from code.
  • Easily changing settings without recompiling.
  • Securely storing secrets (the file is usually not committed to the repository).

In Go, libraries like godotenv are often used to load variables from a .env into the environment:

import (
    "fmt"
    "os"
    "github.com/joho/godotenv"
)

func main() {
    err := godotenv.Load()
    if err != nil {
        fmt.Println("Error loading .env file")
    }
    dbUser := os.Getenv("DB_USER")
    fmt.Println("Database user is", dbUser)
}
What is a .env file and what is it used for? — Golang - sobes.tech