DEV Community

Cover image for Let's write config for your Golang web app on right way — YAML 👌

Let's write config for your Golang web app on right way — YAML 👌

Vic Shóstak on February 13, 2020

Introduction Hello, everyone! 😉 Today, I would like to discuss about configuration for web application on Golang. And not just talk, but...
Collapse
 
j4ng5y profile image
Jordan Gregory • Edited

I truly appreciate the contribution to the community. That said though, as this feels targeted at newcomers, I personally wouldn't teach "global variables" or "using the init()" function if I could avoid it. Later in life, those two constructs make it harder to test and much harder to find a certain class of bugs, especially when the code base gets a lot bigger. Feel free to ignore me though lol, just my $0.02.

Collapse
 
koddr profile image
Vic Shóstak • Edited

Didn't really understand, when init() became an anti-pattern for the Go community? Give me a link to an article about it, please. Same thing about "global variables".

Maybe you should write the right article about how to make a Go web app config in the right format? I'd read it, really.

Feel free to ignore me though lol, just my $0.02.

I don't have the slightest idea what you're talking about here. Explain, please. I haven't even met you to ignore you. 🤷‍♂️

Collapse
 
j4ng5y profile image
Jordan Gregory

Rather than duplicating the work, I'll just give you an MR on your repo with reference :)

As far as ignoring me, I'm opinionated, so it comes with the territory lol.

Thread Thread
 
koddr profile image
Vic Shóstak • Edited

Oh, that's would be nice! Thx 😉

But, actually, when init() become an "anti-pattern"? Because I see init() on many online books, courses and articles by Go bloggers.

I googled it, but I couldn't find any confirmation of your words.

Even the other way around! For example, "Effective Go" book on official Golang website: golang.org/doc/effective_go.html#init

Collapse
 
dadatuputi profile image
Bradford

Good article, thank you. I'm working on a personal project and using this opportunity to learn go. I followed your guide mostly, except I have two different configuration files. I wanted to avoid rewriting code, and I was able to abstract away the type of config struct from the YML parsing function using an interface{} as an additional parameter. It worked well for me, you can see it here. Thanks again.

Collapse
 
koddr profile image
Vic Shóstak

Thanks for reply! 😉 Yep, separated configs are very helpful.

Interesting project, btw! Keep going 👍

Collapse
 
1oglop1 profile image
Jan Gazda

Thanks for this article! It helped me to learn some Go.
Is there any way to set required fields in the config?