Skip to content

Instantly share code, notes, and snippets.

@Develp10
Created April 15, 2022 08:41
Show Gist options
  • Select an option

  • Save Develp10/238ee5ad71d6f2a44b7095568df438db to your computer and use it in GitHub Desktop.

Select an option

Save Develp10/238ee5ad71d6f2a44b7095568df438db to your computer and use it in GitHub Desktop.
package config
import (
"github.com/spf13/viper"
)
type Config struct {
Port string `mapstructure:"PORT"`
DBHost string `mapstructure:"DB_HOST"`
DBUser string `mapstructure:"DB_USER"`
DBPass string `mapstructure:"DB_PASS"`
DBName string `mapstructure:"DB_NAME"`
DBPort string `mapstructure:"DB_PORT"`
}
func LoadConfig() (c Config, err error) {
viper.AddConfigPath("./")
viper.SetConfigName(".env")
viper.SetConfigType("env")
viper.AutomaticEnv()
err = viper.ReadInConfig()
if err != nil {
return
}
err = viper.Unmarshal(&c)
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment