✨Feat: 从.env文件中加载变量
This commit is contained in:
@@ -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=
|
||||||
@@ -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() {
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user