Cookiecutter Go Template

Build status Documentation Status

Opinionated cookiecutter template for golang projects.

The key features of this template include:

  • Support for golang 1.11+ modules

  • Pre-commit hooks via pre-commit including:

    • gofmt to automatically format source files

    • govet to statically lint source files

    • golint to lint source files

    • goimports to check imports

    • gocyclo to check for too complex functions

    • go build to check successful builds

  • Automatic testing and deployment of binaries to GitHub releases via travis-ci.com

  • Pre configured repository including sample tests, a nice README and contribution guide and more

  • Measuring test coverage for codecov.io

  • bump2version for version management

  • Dockerfile for deploying binaries as containers

Quickstart

Prerequisites

Before you get started, make sure you have installed the following tools:

$ python3 -m pip install -U cookiecutter>=1.4.0
$ python3 -m pip install pre-commit bump2version invoke ruamel.yaml halo
$ go get -u golang.org/x/tools/cmd/goimports
$ go get -u golang.org/x/lint/golint
$ go get -u github.com/fzipp/gocyclo/cmd/gocyclo
$ go get -u github.com/mitchellh/gox  # if you want to test building on different architectures

Remember: To be able to excecute the tools downloaded with go get, make sure to include $GOPATH/bin in your $PATH. If echo $GOPATH does not give you a path make sure to set it (export GOPATH="$HOME/go" for example). On order for your changes to persist, do not forget to add these to your shells .bashrc.

Create a new go project

Navigate to wherever you want your project to be created, and run cookiecutter (you will be asked for the project name etc):

$ cookiecutter https://github.com/romnnn/cookiecutter-go.git

After your project was created:

  • Create a remote repository and publish your project:

    $ cd <mypackage>
    $ git remote add origin git@github.com:myusername/mypackage.git
    $ git add .
    $ pre-commit run --all-files
    $ git add .
    $ git commit -m "Initial commit"
    $ git push --set-upstream origin master
    
  • Add the repo to your Travis-CI account. If you have connected travis with GitHub this is done automatically.

  • If you choose tool or both for your project and want to publish pre built release binaries, install the Travis CLI and run:

    $ travis login                  # Login with your GitHub credentials
    $ travis setup releases         # When using travis.org
    $ travis setup releases --com   # When using travis.com
    

    to automatically encrypt a GitHub OAuth token into your .travis.yml config.

    Unfortunately, the travis cli tool appends the token at the config’s top level, so you need to manually edit the .travis.yml config or run:

    $ invoke fix-token
    

    Now you can push the updated .travis.yml to your remote repository:

    $ git add .travis.yml
    $ git commit -m "Add GitHub releases token"
    $ git push
    

    If you do not want to publish pre-built releases, remove the Publish release stage in .travis.yml.

  • If you want to publish the tool as a docker container, connect hub.docker.com with your GitHub account and create a new repository.

    Make sure to choose a matching name and connect your GitHub repository at the bottom of the page. You must also specify the location of the Dockerfile (choose the default /). When you are done click Create and build.

  • Start coding!:

    $ go build <your-package>
    $ invoke run -- --name Roman  # If you chose "tool" or "both"
    $ pre-commit run --all-files
    
  • Release new versions of your package by pushing a new tag to master:

    $ bump2version (major | minor | patch)
    $ git push --follow-tags
    

Documentation

If you need more guidance I encourage you to have a look at the more extensive documentation.