some description.
-
-
Save sko00o/2f1d5ce9595dcead03a65b927158eb8f to your computer and use it in GitHub Desktop.
[MyGoProjectTemplate] #Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PROJPATH := . | |
PROJECTNAME := demo | |
PROJECTVER := 0.0.1 | |
FILES := README.md CHANGELOG.md | |
GO_ENV := CGO_ENABLED=0 GOPROXY=https://goproxy.io | |
GO_EXTRA_BUILD_ARGS := -trimpath |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"time" | |
) | |
// set by the compiler | |
var timestamp, version, revision string | |
func main() { | |
if timestamp == "" { | |
timestamp = time.Now().String() | |
} | |
if revision == "" { | |
revision = "unknown" | |
} | |
if version == "" { | |
version = "dev" | |
} | |
// do your process. | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
include .env | |
# Go related variables. | |
GOBASE := $(shell pwd) | |
GOBIN := $(GOBASE)/build | |
PUBDIR := $(GOBASE)/dist | |
TESTOUT := $(GOBASE)/coverage.out | |
TIMESTAMP := "$(shell date --rfc-3339='seconds')" | |
PKGS := $(shell go list ./... | grep -v /vendor) | |
ifeq (,$(wildcard .git)) | |
REVER := $(shell svnversion -cn | sed -e "s/.*://" -e "s/\([0-9]*\).*/\1/" | grep "[0-9]") | |
else | |
REVER := $(shell git describe --always | sed -e "s/^v//") | |
endif | |
PROJECTBASE := $(GOBASE)/$(PROJPATH) | |
PROJECTNAME ?= $(shell basename "$(PROJECTBASE)") | |
OUTFILE := "$(PROJECTNAME)_$(shell go env GOARCH)_v$(PROJECTVER)-$(shell date +%Y%m%d%H%M%S)" | |
# Make is verbose in Linux. Make it silent. | |
MAKEFLAGS += --silent | |
.PHONY: all help | |
all: help | |
help: Makefile | |
@echo | |
@echo " Choose a command run in "$(PROJECTNAME)":" | |
@echo | |
@sed -n 's/^##//p' $< | column -t -s ':' | sed -e 's/^/ /' | |
@echo | |
## build: compiling from source | |
.PHONY: build | |
build: generate | |
@echo ┌ compiling from source | |
mkdir -p $(GOBIN) | |
$(GO_ENV) go build $(GO_EXTRA_BUILD_ARGS) \ | |
-ldflags " \ | |
-X 'main.timestamp="$(TIMESTAMP)"' \ | |
-X 'main.version=$(PROJECTVER)' \ | |
-X 'main.revision=$(REVER)' \ | |
" \ | |
-o $(GOBIN)/$(PROJECTNAME) \ | |
$(PROJECTBASE)/main.go | |
@echo └ done | |
## compress: compressing for release | |
.PHONY: compress | |
compress: build | |
@echo ┌ compressing for release | |
mkdir -p $(PUBDIR) | |
tar czf $(PUBDIR)/$(OUTFILE).tar.gz \ | |
-C $(GOBIN) $(PROJECTNAME) \ | |
-C $(GOBASE) $(FILES) | |
@echo └ done | |
## clean: cleanup workspace | |
.PHONY: clean | |
clean: | |
@echo ┌ cleanup workspace | |
@rm -rf $(GOBIN) | |
@rm -rf $(PUBDIR) | |
@rm -f $(TESTOUT) | |
@echo └ done | |
## test: running tests | |
.PHONY: test | |
test: lint | |
@echo ┌ running tests | |
@rm -f $(TESTOUT) | |
@go test -p 1 -v \ | |
-cover $(PKGS) \ | |
-coverprofile $(TESTOUT) | |
@echo └ done | |
## lint: running code inspection | |
.PHONY: lint | |
lint: | |
@echo ┌ running code inspection | |
@for pkg in $(PKGS) ; do \ | |
golint $$pkg ; \ | |
done | |
@go vet $(PKGS) | |
@echo └ done | |
.PHONY: generate | |
generate: | |
go generate config/config.go | |
# shortcuts for development | |
.PHONY: requirements | |
requirements: | |
@echo ┌ setup development requirements | |
go install golang.org/x/lint/golint | |
@echo └ done | |
.PHONY: serve | |
serve: build | |
@echo start $(PROJECTNAME) | |
$(GOBIN)/$(PROJECTNAME) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment