load_config.go 463 B

1234567891011121314151617181920212223
  1. package config
  2. import (
  3. "fmt"
  4. "github.com/mitchellh/mapstructure"
  5. "github.com/spf13/viper"
  6. )
  7. func Initialize(path string, cfgStruct interface{}) error {
  8. dir := fmt.Sprintf("./config/%s", path)
  9. viper.SetConfigType("yaml")
  10. viper.SetConfigFile(dir)
  11. if err := viper.ReadInConfig(); err != nil {
  12. return err
  13. }
  14. if err := viper.Unmarshal(&cfgStruct, func(c *mapstructure.DecoderConfig) {
  15. c.TagName = "yaml"
  16. }); err != nil {
  17. return err
  18. }
  19. return nil
  20. }