cross platform path to store config file

538 views
Skip to first unread message

Josh Kamau

unread,
Jan 30, 2015, 12:29:40 PM1/30/15
to golang-nuts
Hello;

Whats the best place to store config files that will work for window, linux and osx?

The app will run as a service and its not able to read evironment variables.

Thanks.
Josh

Konstantin Khomoutov

unread,
Jan 30, 2015, 12:46:52 PM1/30/15
to Josh Kamau, golang-nuts
I'm afraid, there isn't such a name.

On Windows, a program's config file is typically located along with the
program's binary itself, in a folder named like
"%ProgramFiles%\{Vendor}\{Program}"

On a typical Linux-based system a program's config file is expected to
be located under /etc, and have a meaningful name, like
"/etc/{progname}.conf".
*But* bevare that this Unix-y concept of having the program's files
*spread* across the system relies heavily on the concept of packages:
that is, every program gets installed into the system from a package,
and through this, the system's package manager knows how to delete all
that files when you request the package removal. So if you do package
your program, all is well, but if you employ frequent/rapid
"bare" deployments, consider breaking these LFS rules and place all
your program's static (i.e. changed by hand only) files under
"/opt/{progname}" and have it place the stuff it changes itself under
"/srv/{progname}".

I can't say anything about OS X.

Since now you have to deal with different locations on different
platforms, consider using platform_specific files, like:

conf_linux.go:

func GetConfigPath() {
return "/etc/foo.conf"
}

conf_windows.go:

func GetConfigPath() {
// 1) Figure out the name of the executable;
// 2) Get its directory name (use path.filepath);
// 3) Append "foo.conf" to it (use path.filepath);
return theResult;
}

...

Josh Kamau

unread,
Jan 30, 2015, 1:27:50 PM1/30/15
to Konstantin Khomoutov, golang-nuts
Thanks Konstantin; 

I will follow your advice.

Josh

Inando

unread,
Jan 31, 2015, 2:18:40 AM1/31/15
to golan...@googlegroups.com
Don't write in the ProgramFiles folder in windows. You may not have permissions and it is the wrong folder for settings.
Can't you use the ALLUSERSPROFILE variable?

Craig Weber

unread,
Feb 1, 2015, 4:02:30 PM2/1/15
to golan...@googlegroups.com, joshn...@gmail.com
The closest thing I know about is the XDG specification: http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html

This is basically what Ubuntu (and probably many others) use, though it should be portable to OSX and Windows (although it may not be idiomatic).

Tim Shannon

unread,
Feb 2, 2015, 9:56:04 AM2/2/15
to golan...@googlegroups.com, joshn...@gmail.com
Here's the small library I wrote to handle my config files (they are just simple json files): https://bitbucket.org/tshannon/config

You could check out the location_<platform>.go files to see my best guess as to the standard locations for config files:


Hope it helps, and if anyone sees any corrections, let me know.

Craig

unread,
Feb 2, 2015, 9:59:49 AM2/2/15
to Tim Shannon, golang-nuts, joshn...@gmail.com
There's also Viper--I don't know much about it, and it's probably pretty heavy-handed, but it's probably good to know that it exists if nothing else:


--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/6zhuuYpCg8Y/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages