Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should resources be kept in golang

Tags:

go

My application uses json configuration files and other resources. Where should I place them in my project hierarchy? I could not find the answer in http://golang.org/doc/code.html (How to Write Go Code)

Upd: The question is not about automatic distribution of resources with application but much simpler: Where should I keep my resources in project hierarchy? Is there some standard place anyone expects them to be?

like image 837
Alexander Ponomarev Avatar asked Oct 25 '13 10:10

Alexander Ponomarev


People also ask

What is CMD folder in Go?

/cmd. This folder contains the main application entry point files for the project, with the directory name matching the name for the binary. So for example cmd/simple-service meaning that the binary we publish will be simple-service .

Where do you put go projects?

A better way to organize a Go project is to put relevant Go code files into a subdirectory under the main directory for the project so that other parts of the project are able to find the APIs and use them. Keeping all source files under the same directory is not a very good idea, even though you can do it.


1 Answers

There is no single correct answer, nor are there any strong conventions assumed or enforced by any Go tooling at this time.

Typically I start by assuming that the files I need are located in the same directory from where the program will be run. For instance, suppose I need conf.json for myprog.go; then both of those files live together in the same directory and it works to just run something like

go build -o myprog && ./myprog 

When I deploy the code, the myprog binary and conf.json live together on the server. The run/supervisor script needs to cd to that directory and then run the program.

This also works when you have a lot of resources; for instance, if you have a webserver with JS, CSS, and images, you just assume they're relative to cwd in the code and deploy the resource directories along with the server binary.

Another alternative to assuming a cwd is to have a -conf flag which the user can use to specify a configuration file. I typically use this for distributing command-line tools and open-source server applications that require a single configuration file. You could even use an -assets flag or something to point to a whole tree of resource files, if you wanted.

Finally, one more approach is to not have any resource files. go-bindata is a useful tool that I've used for this purpose -- it just encodes some data as bytes into a Go source file. That way it's all baked into your binary. I think this method is most useful when the resource data will rarely or never change, and is pretty small. (Otherwise you're going to be shipping around huge binaries.) One (kind of silly) example of when I've used go-bindata in the past was for baking a favicon into a really simple server which didn't otherwise require any extra files besides the server binary.

like image 87
Caleb Spare Avatar answered Sep 18 '22 21:09

Caleb Spare