load_config.go 544 B

123456789101112131415161718192021222324
  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. // dir = fmt.Sprintf("D:\\project\\golangNew\\kpt-tmr-group\\config\\%s", path)
  10. viper.SetConfigType("yaml")
  11. viper.SetConfigFile(dir)
  12. if err := viper.ReadInConfig(); err != nil {
  13. return err
  14. }
  15. if err := viper.Unmarshal(&cfgStruct, func(c *mapstructure.DecoderConfig) {
  16. c.TagName = "yaml"
  17. }); err != nil {
  18. return err
  19. }
  20. return nil
  21. }