✨Feat: 从.env文件中加载变量

This commit is contained in:
2026-03-22 20:54:09 +08:00
parent 295938c916
commit 81b8933c83
3 changed files with 30 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
module agent
go 1.25.1
require github.com/joho/godotenv v1.5.1
+2
View File
@@ -0,0 +1,2 @@
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
+23
View File
@@ -0,0 +1,23 @@
package main
import (
"fmt"
"os"
"github.com/joho/godotenv"
)
func init() {
err := godotenv.Load()
if err != nil {
fmt.Println(".env 文件加载失败")
}
apiKey := os.Getenv("API_KEY")
baseUrl := os.Getenv("BASE_URL")
fmt.Printf("API Key: %s\n", apiKey)
fmt.Printf("Base URL: %s\n", baseUrl)
}
func main() {
}