Giter Site home page Giter Site logo

hellogopher's Introduction

Makefile for Go projects (1.16+)

This is an example of Makefile to build a Go project. This is quite similar to Filippo Valsorda's hellogopher.

Initially, this is for people who don't know about GOPATH or who don't want to use it (like me). However, starting with Go 1.11, modules enable to work outside of GOPATH without any special environment. This turns this Makefile as only a convenience tool.

This Makefile may not be used as is. It is expected to be modified to fit your needs. See Akvorado's Makefile for an example on a more complex project.

Dependencies

This example relies on modules to retrieve dependencies. This requires use of Go 1.11 or more recent. To update a dependency, use go get DEPENDENCY@REVISION.

We also rely on go install TOOL@latest which requires Go 1.16. You can checkout tag v0.6 if you need compatibility up to Go 1.11.

Some tools now require more recent versions of Go (1.18+), but it would be possible to pin them to older versions.

On first build, you need to run go mod init PROJECTNAME.

Versioning

Version is extracted from git tags using anything prefixed by v.

Usage

The following commands are available:

  • make help to get help
  • make to build the binary (in bin/)
  • make test to run tests
  • make test-verbose to run tests in verbose mode
  • make test-race for race tests
  • make test-coverage for test coverage (will output coverage.html and coverage.xml in test/.
  • make test PKG=helloworld/hello to restrict test to a package
  • make clean
  • make lint to run golint
  • make fmt to run gofmt

The very first line of the Makefile is the most important one: this is the path of the package. I don't use a go getable package path but you can.

Be sure to browse the remaining of the Makefile to understand what it does. There are some tools that will be downloaded. You can use already-installed one by specifying their full path this way instead:

make lint GOLINT=/usr/bin/golint

Files other than .gitignore and Makefile are just examples.

Misc

If golint complains with go/build: importGo hellogopher/cmd: exit status 2, just delete it from bin/ and let the Makefile build it again.

If you prefer, you can also include this Makefile into another one. Rename it to hellogopher.mk and put in Makefile something like this:

include hellogopher.mk

# Your custom settings
TIMEOUT=10

# Your custom rules
doc: ; $(info $(M) build documentation) @ ## Build documentation
	$(MAKE) -C doc

License

This Makefile is published under the CC0 1.0 license. See LICENSE for more details.

hellogopher's People

Contributors

2sec4u avatar dependabot[bot] avatar marccardinal avatar vincentbernat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

hellogopher's Issues

`make test-xml` fails when test directory is missing

$ git clone https://github.com/vincentbernat/hellogopher.git
Cloning into 'hellogopher'...
remote: Counting objects: 102, done.
remote: Total 102 (delta 0), reused 0 (delta 0), pack-reused 102
Receiving objects: 100% (102/102), 20.29 KiB | 391.00 KiB/s, done.
Resolving deltas: 100% (50/50), done.
$ cd hellogopher/
$ make test-xml
▶ running gofmt…
▶ setting GOPATH…
▶ building github.com/golang/dep/cmd/dep…
▶ retrieving dependencies…
▶ building github.com/golang/lint/golint…
▶ running golint…
▶ building github.com/tebeka/go2xunit…
▶ running tests…
tee: test/tests.output: No such file or directory
=== RUN   TestHello
--- PASS: TestHello (0.00s)
PASS
ok      hellogopher/hello       (cached)
make: *** [test-xml] Error 1

`lint` target and `/dev/stderr` permissions

Hello! I spot an issue recently, when make lint is run as some non-privileged user like nobody or gitlab-runner it displays the following error messages:

▶ running golint…
tee: /dev/stderr: Permission denied
tee: /dev/stderr: Permission denied
tee: /dev/stderr: Permission denied
tee: /dev/stderr: Permission denied
tee: /dev/stderr: Permission denied
tee: /dev/stderr: Permission denied
tee: /dev/stderr: Permission denied
tee: /dev/stderr: Permission denied
tee: /dev/stderr: Permission denied
tee: /dev/stderr: Permission denied
tee: /dev/stderr: Permission denied
tee: /dev/stderr: Permission denied
Makefile:131: recipe for target 'lint' failed
make: *** [lint] Error 1

Using goimports instead of gofmt

Hello! Thank you so much for the fantastic Makefile!

I've faced some strange issue and I hope you could help me. I'm trying to use goimports instead of gofmt. So I changed a Makefile a bit:

diff --git a/Makefile b/Makefile
index b295c2a..b14c901 100644
--- a/Makefile
+++ b/Makefile
@@ -10,7 +10,7 @@ TESTPKGS = $(shell env GOPATH=$(GOPATH) $(GO) list -f '{{ if .TestGoFiles }}{{ .
 
 GO      = go
 GODOC   = godoc
-GOFMT   = gofmt
+GOFMT   = goimports
 GLIDE   = glide
 TIMEOUT = 15
 V = 0

To check if it works I changed cmd/hello.go a bit:

diff --git a/cmd/hello.go b/cmd/hello.go
index 05d740b..ab8ca8c 100644
--- a/cmd/hello.go
+++ b/cmd/hello.go
@@ -4,8 +4,6 @@ import (
 	"os"
 
 	"github.com/spf13/cobra"
-
-	"hellogopher/hello"
 )
 
 func init() {
@@ -13,7 +11,7 @@ func init() {
 }
 
 var helloCmd = &cobra.Command{
-	Use:   "hello",
+	Use: "hello",
 	Short: "Say hello",
 	Long:  `Print a nice hello message on the standard output.`,
 	Run: func(cmd *cobra.Command, args []string) {

The I run make fmt:

$ make fmt
▶ running gofmt…
/tmp/hellogopher/cmd/hello.go

Well, goimports fixed formatting but haven't added the missing import:

diff --git a/cmd/hello.go b/cmd/hello.go
index 05d740b..900c82f 100644
--- a/cmd/hello.go
+++ b/cmd/hello.go
@@ -4,8 +4,6 @@ import (
 	"os"
 
 	"github.com/spf13/cobra"
-
-	"hellogopher/hello"
 )
 
 func init() {

Do you have any idea on how it could be improved?

Thanks!

feature: build static task

Could we implement a task for the Makefile to do something like CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo .?

This helps to build a static binary for a docker image that has no dynamics.

Using Makefile as an include

Hi @vincentbernat if I use your Makefile as an included i.e. your content in common.mk

include ./common.mk

Then running make help the awk fs will not match correctly

$ make help
common.mk      Build program binary
common.mk      Run benchmarks
common.mk      Run only short tests
common.mk      Run tests in verbose mode with coverage reporting
common.mk      Run tests with race detector
common.mk      Run tests
common.mk      Run tests with xUnit output
common.mk      Run coverage tests
common.mk      Run golint
common.mk      Run gofmt on all source files
common.mk      Cleanup everything

How to use go generate?

I have one file with //go generate: go run gen.go and that gen.go file imports one package from vendor. I added the following target to my Makefile:

.PHONY: generate
generate: vendor | $(BASE) ; $(info $(M) running go generate…) @ ## Run go generate
	$Q cd $(BASE) && $(GO) generate ./...

However, when I try to run make generate I get the following error:

$ make generate
▶ running go generate…
gen.go:30:2: cannot find package "github.com/foo/bar" in any of:
        /usr/lib/go-1.10/src/github.com/foo/bar (from $GOROOT)
        /tmp/tidal/.gopath~/src/github.com/foo/bar (from $GOPATH)
pkg/suboackage/source.go:21: running "go": exit status 1
make: *** [Makefile:154: generate] Error 1

Please advice how to use go generate.

date issue in osx

Checked out and run on OSX.

$ make
date: illegal option -- I
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
▶ running gofmt…
▶ setting GOPATH…
▶ retrieving dependencies…
▶ building golint…
▶ running golint…
▶ building executable…

feature request

This is pretty useful.

This being gopath independent makes it much easier to fork a github repo, clone it to your harddisk and work on it, WITHOUT affected the upstream version you also have on your disk.

Now i am thinking that it woudl be very nice to add some basic standards typical git commands that Maintainer and a Contributor needs.
A Maintainer is likely to be working against the man git repo, while a Contributor is working on a fork of the main repo.

What do you think ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.