We have many Devs that are used to using .env files but we don't use them in production and instead set the ENV safely. We therefor use the godotenv module but only include it during dev using go tags. Here is the code used:
//go:build dev
// +build dev
package main
import (
"log"
"github.com/joho/godotenv"
)
// We want to use a .env in development but
// not in production. This will only be used
// when using "air" or "make run"
func init() {
// Load .env file in development
if err := godotenv.Load(); err != nil {
log.Fatalf("Error loading .env file: %v", err)
}
}
This works great when we run using go build --tags dev .
It works exactly as expected and godotenv isn't including when we build for production. One problem is that we have a /healthz endpoint in our application which part of it checks if env variables are set. The problem we have are having is that when we run our go tests either go test --tags dev ./...
or go test ./...
the .env isn't used. As I and my colleagues understand things this should work. All our test does is check that healthz endpoint and it fails. While checking the same endpoint in the same fashion when running go build (with the tag) works.
So what am I missing. Why isn't go test respecting tags or is it that the tags for go test are specific to just test files? How would I include the main program tags?