This is to know the different ways to read environment variables in Golang
Open the terminal in the project root directory.
go get github.com/joho/godotenvgodotenv provides a Load method to load the env files.
#Load the .env file in the current directory
godotenv.Load()
#or
godotenv.Load(".env")Load method can load multiple env files at once. This also supports yaml.
if you want to read more of godotenv.
Viper is a complete configuration solution for Go applications including 12-Factor apps. It is designed to work within an application and can handle all types of configuration needs and formats. Reading from JSON, TOML, YAML, HCL, envfile and Java properties config files
Open the terminal in the project root directory.
go get github.com/spf13/viperif you want to read more of viper.
- Install pre-commit on your system. You can install it with pip:
pip install pre-commit- Create a .pre-commit-config.yaml file in the root of your project and add the hooks you want to run in the pre-commit. Here is an example:
repos:
- repo: https://github.com/dnephin/pre-commit-golang
rev: v1.3.0
hooks:
- id: go-fmt
- id: go-mod-tidy
- id: go-vet- Know more options See more
- Run the following command to install the hooks in your project:
pre-commit installThis will install the hooks in your repository and associate them with the git commit command.
From now on, when you do a git commit, the hooks configured in .pre-commit-config.yaml will be executed automatically before the commit is performed. If any of the hooks fail, the commit will not be performed.
- Optionally, you can manually execute the pre-commit hooks at any time by running the command:
pre-commit run --all-filesThis will execute all hooks configured in .pre-commit-config.yaml in all repository files.