[golang notes] How to set environment variables for your project
# Dev NoteIntroduction
When developing a project, it’s common to switch between different environments, which often necessitates setting various environment variables. However, hardcoding these values directly into the code can be cumbersome to modify each time. By dynamically passing environment variables, we can reduce unnecessary changes, resulting in cleaner code.
Flag
In Go, you can achieve similar functionality using the flag package. For example:
var env string
var accessToken string
func main() {
flag.StringVar(&env, 'ENV', 'development', 'your current env')
flag.StringVar(&accessToken, 'ACCESS_TOKEN', 'xxx-oo-ooo', 'your API access token')
flag.Parse()
// start your application
}
Alternatively, you can refer to this article that discusses using go build -ldflags to set variables at compile time.
While this approach does allow for not hardcoding values in the program, the variables still need to be defined beforehand.
Loading via YAML
To address the aforementioned issue, we can manage environment variables in a unified way using YAML (or any format you prefer). For example:
func LoadEnv(filename string) bool {
file, err := ioutil.ReadFile(filename)
if err != nil {
// In production, we might set environment variables directly via the console
return false
}
var config = make(map[string]string)
yaml.Unmarshal(file, &config)
for k, v := range config {
os.Setenv(k, v)
}
}
After loading the variables from the YAML file, we store them uniformly using os.Setenv(k, v). This makes it easy to adjust variables in a local environment. Remember to ignore the configuration file in version control; otherwise, if it contains sensitive information and happens to be in a public repository, it could be exposed.
Conclusion
I’ve created a simple repo to handle this task. If you find it useful during development, feel free to optimize it further! XD
Related Posts
- Stop Using Access Keys AlreadyAccess Keys are an easily overlooked security risk on AWS. Use OIDC with IAM Roles so GitHub Actions can securely access AWS resources without any secrets.
- Database Primary Keys: AUTO_INCREMENT, UUID, and UUIDv7Backend developers often have to decide on a primary key: auto increment or UUID? What about collisions? How much faster is UUIDv7 compared with created_at + index? After benchmarking 20 million rows and looking at the design trade-offs, this post gives you the answer.
- Sharing My Experience with ZeaburIndependent developers often choose platforms like Vercel for deploying their services. However, when more advanced requirements arise, such as database connections, Vercel can become less convenient. Additionally, the pricing of typical cloud service providers can be quite expensive for solo developers. In this article, I’ll share some insights on using Zeabur and highly recommend it to everyone!
- Keyboard Enthusiast's Guide - Firmware EditionThis article is part of the IT 2023 Ironman Competition: A Beginner's Guide to Keyboards - Firmware Edition.