Hey there,
I was hoping that you might not mind me asking a few questions and perhaps get your opinion? I wrote an application originally in Python and I thought it would be a good exercise to help me better learn Go to convert it. There were some Python packages I really liked that I used and I have been looking around to try and find similar ones in Go and so far this one looks pretty close to have I was using. (Which was this one here). It was my first time trying to make a full on application, so most of it was slapped together and "worked". This time around I want to try and make it a bit more structured and just, better, lol.
That being said, I was going to have a config.go and in there was going to have some structs to hold the config data so that this time around I am not constantly referencing the data via the actual loading mechanism to the config file. Before I was getting data like this all throughout the application:
config = jsoncfg.load_config('instance/config/config.json')
@staticmethod
def start():
token = config.token()
if token:
# The rest of the code
else:
print("Missing token in config.json")
I wanted to use Yaml this time around for ease of use on the end user side, as my previous config file, while informative, ended up being littered with commends and what not since I was capable of doing so. I have my mockup Yaml file for the main settings config and am my plan was to have a MainSettings struct {}
and figured it might be a good idea to try and structure it similar to how my yaml file was laid out.
Here is a portion of the config file layout:
settings:
system:
token: '12f23f23fFDS32f23123r13r3f'
commandprefix: '!cmd'
requireemail: 'Yes'
consoleloglevel: 'DEBUG'
fileloglevel: "DEBUG"
integrations:
wordpress: 'Yes'
connection: 'Oauth2'
webaddress: 'http://webaddress.com'
server:
id: 321321321321
userids:
- 123123123123
- 123123123123
roles:
Verified: 123123123123
Unverified: 123123123123
I made a struct to follow suit:
type MainSettings struct {
System struct {
Token string
CommandPrefix string
RequireEmail string
ConsoleLogLevel string
FileLogLevel string
}
Integrations struct {
WordPress string
Connection string
WebAddress string
}
Server struct {
Id int
Userids []int
Roles map[string]int
}
}
So my questions are as follows. I am currently attempting to learn Go so I am not 100% certain I am doing this "the right way", but what would be the best way to try and populate the data properly using this config package? For each one field should I assign them, similar to something like this?
func LoadConfig(filename string) MainSettings {
config.WithOptions(config.ParseEnv)
config.AddDriver(yaml.Driver)
err := config.LoadFiles(filename)
if err == nil {
panic(err)
}
// The structure of this was autogenerated by my IDE GoLand, I am hoping this is how you go about it. It feels like it might be a bit much?
mainsettings := MainSettings{System: struct {
Token string
CommandPrefix string
RequireEmail string
ConsoleLogLevel string
FileLogLevel string
}{Token: config.String("settings.system.token"),
CommandPrefix: config.String("settings.system.commandprefix"),
RequireEmail: config.String("settings.system.requireemail"),
ConsoleLogLevel: config.String("settings.system.consoleloglevel"),
FileLogLevel: config.String("settings.system.fileloglevel")}}
return mainsettings
}
}
Do you have any recommendations perhaps if I am going about this incorrectly? I did attempt to run it as is, but unfortunately it came up empty.
func main() {
settings := config.GetConfig()
fmt.Printf("Token: %s", settings.System.Token)
}
I realize this became much longer than I intended, so I apologize about that. I am hoping that perhaps you might be able to at least point me in the right direction?
I appreciate your time,
-MH