Skip to content

DotENV - 环境变量管理

DotENV 用于加载和管理环境变量。

特性

  • 从 .env 文件加载
  • 环境变量解析
  • 类型转换
  • 默认值支持

安装

bash
go get github.com/o-zoox/dotenv

快速开始

基本使用

go
package main

import (
    "fmt"
    "github.com/o-zoox/dotenv"
)

func main() {
    // 加载 .env 文件
    err := dotenv.Load()
    if err != nil {
        panic(err)
    }
    
    // 获取环境变量
    dbHost := dotenv.Get("DB_HOST", "localhost")
    dbPort := dotenv.GetInt("DB_PORT", 5432)
    
    fmt.Printf("Database: %s:%d\n", dbHost, dbPort)
}

.env 文件示例

env
DB_HOST=localhost
DB_PORT=5432
DB_USER=admin
DB_PASSWORD=secret

更多信息

基于 VitePress 构建