package config import ( "github.com/spf13/viper" ) type Config struct { Port uint16 DataService DataService } type DataService struct { IP string Port uint16 Username string Password string Database string } func New() (*Config, error) { viper.SetConfigType("yaml") viper.AddConfigPath(".") viper.SetConfigName("config") if err := viper.ReadInConfig(); err != nil { return nil, err } setDefaults() var config Config err := viper.Unmarshal(&config) return &config, err } func setDefaults() { viper.SetDefault("Port", "8080") viper.SetDefault("DataService.IP", "127.0.0.1") viper.SetDefault("DataService.Port", "27017") viper.SetDefault("DataService.Username", "haveachin") viper.SetDefault("DataService.Password", "") viper.SetDefault("DataService.Database", "scoreboard") }