Skip to content

Instantly share code, notes, and snippets.

@eddiefisher
Last active January 7, 2017 17:22
Show Gist options
  • Save eddiefisher/9779454459aa9dc7bda1540feefacfa4 to your computer and use it in GitHub Desktop.
Save eddiefisher/9779454459aa9dc7bda1540feefacfa4 to your computer and use it in GitHub Desktop.
Golang simple generate a project

###set global $GOPATH ~/.zshrc && add bin folder

export GOPATH="$HOME/projects/golang"
export PATH="$GOPATH/bin:$PATH"

###install gonew & golint

go get github.com/bmatsuo/gonew
go get github.com/golang/lint/golint

###install direnv

brew install direnv

ZSH Add the following line at the end of the "~/.zshrc" file:

eval "$(direnv hook zsh)"

edit config file, change user information & ExternalTemplates path add template file to templates folder

####in Makefile must be tabs, not spaces

new project:

gonew cmd goplay1
{
"Default": {
"Environment": "default",
"Project": "lib"
},
"Environments": {
"default": {
"BaseImportPath": "github.com/eddiefisher",
"Inherits": null,
"User": {
"Name": "Eddie Fisher",
"Email": "eddi.fisher@gmail.com"
}
}
},
"ExternalTemplates": [
"_____$GOPATH_____/src/github.com/bmatsuo/gonew/templates"
],
"Projects": {
"cmd": {
"Inherits": [
"git",
"newbsd",
"travis",
"makefile",
"envrc"
],
"Hooks": {
"Pre": null,
"Post": null
},
"Files": {
"Git-Ignore": {
"Path": "{{.Project.Name}}/.gitignore",
"Type": "other",
"Templates": [
"other.gitignore.t2"
]
},
"Main": {
"Path": "{{.Project.Name}}/src/{{.Package}}.go",
"Type": "go",
"Templates": [
"license.newbsd.gohead.t2",
"go.cmd.t2"
]
},
"Bin": {
"Path": "{{.Project.Name}}/bin/.gitkeep",
"Type": null
},
"README": {
"Path": "{{.Project.Name}}/README.md",
"Type": "readme",
"Templates": [
"README.md.t2",
"license.newbsd.readme.t2"
]
}
}
},
"cmdtest": {
"Inherits": [
"cmd"
],
"Hooks": {
"Pre": null,
"Post": null
},
"Files": {
"Test": {
"Path": "{{.Project.Name}}/{{.Package}}_test.go",
"Type": "go",
"Templates": [
"license.newbsd.gohead.t2",
"go.cmdtest.t2"
]
}
}
},
"git": {
"Inherits": null,
"Hooks": {
"Pre": null,
"Post": [
{
"Cwd": "{{.Project.Name}}",
"Commands": [
"git init",
"git add .",
"git commit -m '{{.Project.Name}} created {{date}} by gonew'"
]
}
]
},
"Files": {}
},
"lib": {
"Inherits": null,
"Hooks": {
"Pre": null,
"Post": null
},
"Files": {
"Main": {
"Path": "{{.Project.Name}}.go",
"Type": "go",
"Templates": [
"license.newbsd.gohead.t2",
"go.lib.t2"
]
}
}
},
"libtest": {
"Inherits": [
"lib"
],
"Hooks": {
"Pre": null,
"Post": null
},
"Files": {
"Test": {
"Path": "{{.Project.Name}}_test.go",
"Type": "go",
"Templates": [
"license.newbsd.gohead.t2",
"go.pkgtest.t2"
]
}
}
},
"mit": {
"Inherits": null,
"Hooks": {
"Pre": null,
"Post": null
},
"Files": {
"License": {
"Path": "{{.Project.Name}}/LICENSE",
"Type": "licenses",
"Templates": [
"license.mit.t2"
]
}
}
},
"newbsd": {
"Inherits": null,
"Hooks": {
"Pre": null,
"Post": null
},
"Files": {
"License": {
"Path": "{{.Project.Name}}/LICENSE",
"Type": "licenses",
"Templates": [
"license.newbsd.t2"
]
}
}
},
"pkg": {
"Inherits": [
"git",
"newbsd",
"travis"
],
"Hooks": {
"Pre": null,
"Post": null
},
"Files": {
"Git-Ignore": {
"Path": "{{.Project.Name}}/.gitignore",
"Type": "other",
"Templates": [
"other.gitignore.t2"
]
},
"Main": {
"Path": "{{.Project.Name}}/{{.Package}}.go",
"Type": "go",
"Templates": [
"license.newbsd.gohead.t2",
"go.pkg.t2"
]
},
"README": {
"Path": "{{.Project.Name}}/README.md",
"Type": "readme",
"Templates": [
"README.md.t2",
"license.newbsd.readme.t2"
]
}
}
},
"pkgtest": {
"Inherits": [
"pkg"
],
"Hooks": {
"Pre": null,
"Post": null
},
"Files": {
"Test": {
"Path": "{{.Project.Name}}/{{.Package}}_test.go",
"Type": "go",
"Templates": [
"license.newbsd.gohead.t2",
"go.pkgtest.t2"
]
}
}
},
"travis": {
"Inherits": null,
"Hooks": {
"Pre": null,
"Post": null
},
"Files": {
"Travis": {
"Path": "{{.Project.Name}}/.travis.yml",
"Type": "other",
"Templates": [
"travis.yml.t2"
]
}
}
},
"envrc": {
"Inherits": null,
"Hooks": {
"Pre": null,
"Post": [
{
"Cwd": "{{.Project.Name}}",
"Commands": [
"direnv allow ."
]
}
]
},
"Files": {
"Envrc": {
"Path": "{{.Project.Name}}/.envrc",
"Type": "other",
"Templates": [
"other.envrc.t2"
]
}
}
},
"makefile": {
"Inherits": null,
"Hooks": {
"Pre": null,
"Post": null
},
"Files": {
"Makefile": {
"Path": "{{.Project.Name}}/Makefile",
"Type": "other",
"Templates": [
"other.makefile.t2"
]
}
}
},
"vendor_folder": {
"Inherits": null,
"Hooks": {
"Pre": null,
"Post": null
},
"Files": {
"VendorFolder": {
"Path": "{{.Project.Name}}/_vendor/.gitkeep",
"Type": null
}
}
},
"docs_folder": {
"Inherits": null,
"Hooks": {
"Pre": null,
"Post": null
},
"Files": {
"DocsFolder": {
"Path": "{{.Project.Name}}/docs/.gitkeep",
"Type": null
}
}
}
}
}
{{ template "go._head.t2" $ }}
package main
{{ import "fmt" }}
var (
Version string
Build string
)
func main() {
fmt.Println("Version: ", Version)
fmt.Println("Built Time: ", Build)
fmt.Println("Hello, {{.Env.User.Name}}!")
}
.PHONY: build doc fmt lint test vendor_clean vendor_get vendor_update
# This how we want to name the binary output
BINARY={{.Package}}
# These are the values we want to pass for VERSION and BUILD
VERSION=1.0.0
BUILD=`git rev-parse HEAD`
# Setup the -ldflags option for go build here, interpolate the variable values
LDFLAGS=-ldflags "-X main.Version=${VERSION} -X main.Build=${BUILD}"
# Prepend our _vendor directory to the system GOPATH
# so that import path resolution will prioritize
# our third party snapshots.
GOPATH := ${PWD}/_vendor:${GOPATH}
export GOPATH
# Default target
.DEFAULT_GOAL: $(BINARY)
# Builds the project
$(BINARY):
go build ${LDFLAGS} -o ./bin/${BINARY} ./src/${BINARY}.go
# https://github.com/golang/lint
# go get github.com/golang/lint/golint
lint:
golint ./src
# http://golang.org/cmd/go/#hdr-Run_gofmt_on_package_sources
fmt:
go fmt ./src/...
test:
go test ./src/...
# Installs our project: copies binaries
install:
go install ${LDFLAGS} -o ./bin/${BINARY}
# Cleans our project: deletes binaries
clean:
if [ -f ./bin/${BINARY} ] ; then rm ./bin/${BINARY} ; fi
doc:
godoc -http=:6060 -index
vendor_clean:
rm -dRf ./_vendor/src
# We have to set GOPATH to just the _vendor
# directory to ensure that `go get` doesn't
# update packages in our primary GOPATH instead.
# This will happen if you already have the package
# installed in GOPATH since `go get` will use
# that existing location as the destination.
vendor_get: vendor_clean
GOPATH=${PWD}/_vendor go get -d -u -v \
github.com/jpoehls/gophermail \
github.com/codegangsta/martini
vendor_update: vendor_get
rm -rf `find ./_vendor/src -type d -name .git` \
&& rm -rf `find ./_vendor/src -type d -name .hg` \
&& rm -rf `find ./_vendor/src -type d -name .bzr` \
&& rm -rf `find ./_vendor/src -type d -name .svn`
export GOPATH=${PWD}
export PATH="$GOPATH/bin:$PATH"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment