Good example of makefile / recommended project structure?

4,331 views
Skip to first unread message

brand...@gmail.com

unread,
Jun 23, 2014, 12:06:19 PM6/23/14
to golan...@googlegroups.com
I currently have a working go project, https://code.google.com/p/gocupi/, and the build step is the user manually typing go build main.go, and then running ./main, it seems like that is not the best approach(the executable being called main is not helpful...) I was hoping someone could point me towards a good makefile example / build script that I could use(being cross platform would be good) and/or recommend a better way to organize my project.

Thanks,
Brandon

Sebastien Binet

unread,
Jun 23, 2014, 12:15:50 PM6/23/14
to brand...@gmail.com, golang-nuts
Brandon,
I am using this kind of Makefile:

https://github.com/go-hep/fwk/blob/master/Makefile

so usually, my workflow is:
$ make

to build, install and run the tests.

to only build:
$ make build

in fact, I have now created this little 'mk' command:
https://github.com/sbinet/mk

which generates on the fly a similar makefile than the one in the
go-hep/fwk repository.

so now, in any go-based repository, I can just type:
$ mk

to build, install and test.

I probably should also add an automatic detection of users' Makefile
and pick that one instead...

hth,
-s

Chris Hines

unread,
Jun 23, 2014, 12:40:21 PM6/23/14
to golan...@googlegroups.com, brand...@gmail.com
The recommended project structure is documented at http://golang.org/doc/code.html.

brand...@gmail.com

unread,
Jun 23, 2014, 1:45:10 PM6/23/14
to golan...@googlegroups.com, brand...@gmail.com
One complication is that there is a settings file that the executable needs to run, should I copy that file to $GOBIN\ using the build script so that it is available when the user runs the executable? Previously since I just build the executable in the same directory as the main.go file I could expect other files to be at a constant relative path, what is the best practice to do go install when the executable has some runtime dependencies?

James Bardin

unread,
Jun 23, 2014, 2:14:52 PM6/23/14
to golan...@googlegroups.com, brand...@gmail.com


On Monday, June 23, 2014 1:45:10 PM UTC-4, brand...@gmail.com wrote:
One complication is that there is a settings file that the executable needs to run, should I copy that file to $GOBIN\ using the build script so that it is available when the user runs the executable? Previously since I just build the executable in the same directory as the main.go file I could expect other files to be at a constant relative path, what is the best practice to do go install when the executable has some runtime dependencies?

 
This is a systems configuration/deployment issue. I wouldn't tie this to the build at all. Don't ever depend on relative paths for production, there's too many ways it can break.

James Bardin

unread,
Jun 23, 2014, 2:18:03 PM6/23/14
to golan...@googlegroups.com, brand...@gmail.com
(but I was going to add) You're import path is well defined: can you just use $GOPATH/src/$PACKAGE/config.file?

Sebastien Binet

unread,
Jun 23, 2014, 2:40:36 PM6/23/14
to James Bardin, golang-nuts, brand...@gmail.com
when the sources of the package are meant to be available, I usually go with:
https://github.com/andrebq/gas

otherwise, I reach for go-bindata:
https://github.com/jteeuwen/go-bindata

hth,
-s

>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

egon

unread,
Jun 23, 2014, 2:40:42 PM6/23/14
to golan...@googlegroups.com, brand...@gmail.com
On Monday, 23 June 2014 20:45:10 UTC+3, brand...@gmail.com wrote:
One complication is that there is a settings file that the executable needs to run, should I copy that file to $GOBIN\ using the build script so that it is available when the user runs the executable? Previously since I just build the executable in the same directory as the main.go file I could expect other files to be at a constant relative path, what is the best practice to do go install when the executable has some runtime dependencies?

Embed the config file, then add a flag for printing the config file, and loading a different config file. That way if a user wants to use a different config / modify the defaults he can do something like:

thing -printconfig > myconf.config
thing -config=myconf.config


On Monday, June 23, 2014 11:40:21 AM UTC-5, Chris Hines wrote:
The recommended project structure is documented at http://golang.org/doc/code.html.

On Monday, June 23, 2014 12:06:19 PM UTC-4, brand...@gmail.com wrote:
I currently have a working go project, https://code.google.com/p/gocupi/, and the build step is the user manually typing go build main.go, and then running ./main, it seems like that is not the best approach(the executable being called main is not helpful...) I was hoping someone could point me towards a good makefile example / build script that I could
use(being cross platform would be good) and/or recommend a better way to organize my project.

Rely on the standard practices for using Go. This is a quite good overview http://peter.bourgon.org/go-in-production/

Also when you have a custom build file (not recommended unless the project is really complicated), you can use "go run build.go".

+ egon

Christian Neumann

unread,
Jun 25, 2014, 7:31:57 AM6/25/14
to golan...@googlegroups.com
Hey,

for simple projects, I follow the project structure as recommended in
the Golang docs. But this didn't fit my needs for a more complex
project of mine, for which I use a Makefile to build the app,
dependencies, run tests, build packages and so on:
https://github.com/monsti/monsti/blob/master/Makefile
The Makefile builds the app in a separate GOPATH.

It's just another way to do it. At the moment I'm happy with this
setup, but don't see it as a recommendation. It's not tested that much.

Christian

Am Mon, 23 Jun 2014 09:06:19 -0700 (PDT)
schrieb brand...@gmail.com:
Reply all
Reply to author
Forward
0 new messages