Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environment variables #29

Open
Valery-Lapurin opened this issue Mar 6, 2019 · 0 comments
Open

Environment variables #29

Valery-Lapurin opened this issue Mar 6, 2019 · 0 comments

Comments

@Valery-Lapurin
Copy link

Valery-Lapurin commented Mar 6, 2019

A developer can create local .env file and add it in git ignore.
He can then store environment variables for his local development there (such as secrets).
Docker out of the box supports .env file, so running in a container those are accessible via os.Getenv(). But in order for those to be accessible also in local Go set up, you can do godotenv.Load(".env") first.

If you want to avoid manual call of os.Getenv() for each environment variable + get use of default values + have some support of data types, you can create struct that would contain fields named the same way as your environment variables are named - then you pass the struct to envconfig.Process("", &environmentVariables) which enriches the instance with values via reflection.

package variables

import (
	"github.com/joho/godotenv"
	"github.com/kelseyhightower/envconfig"
)

type Specification struct {
	RABBITMQ_CF_SERVICE_NAME    string `default:"rabbitmqent"`
	RABBITMQ_USERNAME          string `default:"User_01"`
	RABBITMQ_PASSWORD           string `required:"true"`
	RABBITMQ_PORT               int    `default:"5672"`
}

var environmentVariables Specification

func InitInstance() {
	godotenv.Load(".env")
	err := envconfig.Process("", &environmentVariables)
	if err != nil {
		panic(err)
	}
}

func GetInstance() Specification {
	return environmentVariables
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant