godotenv
10,688
Stars
468
Forks
42
Watchers
88
Open Issues
MIT
许可证
Go
语言
2013-07-30
创建时间
2026-08-04
最近更新
项目能做什么
Ruby dotenv 的 Go 语言移植版,用于从 .env 文件加载环境变量。
来源:AI 基于仓库事实生成
· 内容以仓库实际 README 为准
使用说明 查看原始 README 原仓库文档
中文使用说明暂未生成。下面保留原仓库 README 摘录,命令与配置均以原文为准。
原始 README 摘录(英文原文,节选前 3000 字符)
# GoDotEnv  [](https://goreportcard.com/report/github.com/joho/godotenv)
A Go (golang) port of the Ruby [dotenv](https://github.com/bkeepers/dotenv) project (which loads env vars from a .env file).
From the original Library:
> Storing configuration in the environment is one of the tenets of a twelve-factor app. Anything that is likely to change between deployment environments–such as resource handles for databases or credentials for external services–should be extracted from the code into environment variables.
>
> But it is not always practical to set environment variables on development machines or continuous integration servers where multiple projects are run. Dotenv load variables from a .env file into ENV when the environment is bootstrapped.
It can be used as a library (for loading in env for your own daemons etc.) or as a bin command.
There is test coverage and CI for both linuxish and Windows environments, but I make no guarantees about the bin version working on Windows.
## Installation
As a library
```shell
go get github.com/joho/godotenv
```
as a tool dependency:
go >= 1.24
```shell
go get -tool github.com/joho/godotenv/cmd/godotenv
```
or if you want to use it as a bin command
```shell
go install github.com/joho/godotenv/cmd/godotenv@latest
```
## Usage
Add your application configuration to your `.env` file in the root of your project:
```shell
S3_BUCKET=YOURS3BUCKET
SECRET_KEY=YOURSECRETKEYGOESHERE
```
Then in your Go app you can do something like
```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
}
```
If you're even lazier than that, you can just take advantage of the autoload package which will read in `.env` on import
```go
import _ "github.com/joho/godotenv/autoload"
```
While `.env` in the project root is the default, you don't have to be constrained, both examples below are 100% legit
```go
godotenv.Load("somerandomfile")
godotenv.Load("filenumberone.env", "filenumbertwo.env")
```
If you want to be really fancy with your env file you can do comments and exports (below is a valid env file)
```shell
# I am a comment and that is OK
SOME_VAR=someval
FOO=BAR # comments at line end are OK too
export BAR=BAZ
```
Or finally you can do YAML(ish) style
```yaml
FOO: bar
BAR: baz
```
as a final aside, if you don't want godotenv munging your env you can just get a map back instead
```go
var myEnv map[string]string
myEnv, err = godotenv.Read()
s3Bucket := myEnv["S3_BUCKET"]
```
... or from an `io.Reader` instead of a local file
```go
reader := getRemoteFile()
myEnv, err := godotenv.Parse(reader)
```
... or from a `string` if yo
阅读完整原文
项目深度分析 AI 生成
团队级安全 .env 同步与加密分发平台 (类似 Dotenv Vault/Infisical 极简版)
| 产品构想 | 针对 Go 项目及中小团队,提供轻量级 CLI 工具与 SaaS 后端,无缝替代 godotenv 的纯文本加载,实现本地 .env 的自动端到端加密、云端备份与团队成员间安全同步。 |
| 核心功能 | 端到端加密存储、CLI 一键拉取与推送配置、角色权限控制、配置变更审计日志、与现有 godotenv 无缝兼容的 SDK 包装 |
| 目标用户 | 使用 Go 的初创团队、远程协同开发团队、DevOps 负责人 |
| 难度/周期 | 中等 · 2-3 个月 · 成本:极低(主要是 API 服务器、加密认证基础设施及域名成本) |
| 收费模式 | Freemium 模式:个人/开源免费,团队按席位订阅收费(如 $5/人/月) |
| 竞争风险 | 市场已有 Doppler、Infisical 等成熟竞品,获客成本高;安全信任背书难以单人快速建立。 |
| 依据 | 已验证事实 |
.env 安全审计与凭证泄漏扫描 CI 插件
| 产品构想 | 提供针对 Git 仓库内 .env 文件与 Go 代码中环境变量使用的自动化静态代码分析工具与 GitHub Action,检测弱口令、失效环境变量引用及硬编码凭据风险。 |
| 核心功能 | .env 模式验证(类型检查与必填项校验)、未使用的 env 提示、潜在密钥泄露实时拦截、CI/CD 阻塞检查与合规报告生成 |
| 目标用户 | 开源项目维护者、注重安全合规的企业开发组 |
| 难度/周期 | 低 · 1 个月 · 成本:几乎为零(GitHub Marketplace 部署、静态分析逻辑) |
| 收费模式 | GitHub Marketplace 订阅(免费基础版,企业版付费解锁合规报告与自定义规则) |
| 竞争风险 | GitGuardian、Trufflehog 等通用扫描工具覆盖了部分场景,单纯针对 .env 的工具壁垒较低。 |
| 依据 | AI推测 |
类型安全的高性能 Go 环境变量代码生成器 (CLI + 扩展库)
| 产品构想 | 基于 .env 文件或 .env.example 自动生成强类型的 Go 结构体代码与验证器,替代运行时弱类型 map/os.Getenv 调用,减少线上由于环境变量缺失或类型错误导致的运行时崩溃。 |
| 核心功能 | 根据 .env 自动生成 Go struct、类型转换(int, bool, duration等)、启动时强制依赖检查、结合 godotenv 的一键加载插件 |
| 目标用户 | 对系统健壮性要求高的 Go 后端开发团队 |
| 难度/周期 | 低 · 2-4 周 · 成本:零成本(纯本地 CLI 研发) |
| 收费模式 | 开源核心工具 + 赞助(GitHub Sponsors)+ 针对企业的私有配置规范定制咨询 |
| 竞争风险 | 单体工具变现能力弱,开发者倾向于免费开源生态,单纯代码生成工具难形成强付费场景。 |
| 依据 | 数据支持的判断 |
AI 基于仓库公开数据分析生成,含推测成分,不构成收益承诺。
报告会保存到 个人中心
竞品与相似项目 GitHub Search 真实检索
| 项目 | 中文简介 | Stars | 语言 |
|---|---|---|---|
| motdotla/dotenv | 中文介绍暂未生成 Loads environment variables from .env for nodejs projects. |
20,563 | JavaScript |
| phpdotenv/phpdotenv | 中文介绍暂未生成 Loads environment variables from `.env` to `getenv()`, `$_EN |
13,553 | PHP |
| theskumar/python-dotenv | 中文介绍暂未生成 Reads key-value pairs from a .env file and can set them as e |
8,920 | Python |
| bkeepers/dotenv | 中文介绍暂未生成 A Ruby gem to load environment variables from `.env`. |
6,770 | Ruby |
| dotenvx/dotenvx | 中文介绍暂未生成 a secure dotenv—from the creator of `dotenv` |
5,880 | JavaScript |
以上为通过 GitHub 官方 Search API 检索的相似项目,功能差异与优劣势需进一步人工确认,配置 AI 后可生成对比分析。
项目优势与风险
| 技术成熟度 | 创建于 2013-07-30,最近推送 2026-08-04,发布过 13 个 Release |
| 社区活跃 | Star 10,688 / Fork 468 / Issue 88(数据支持,可自行核实) |
| 许可证 | MIT |
| 主要风险 | 上游项目方向可能变化;同赛道竞争者较多(见竞品表);热度与需求不完全等价 |
安装与使用
此项目未提供 Docker 镜像,安装步骤以原仓库 README 为准(涉及依赖安装与环境配置,对新手门槛较高)。