Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Go Cheat Sheet

Go Cheat Sheet

Install Go on Mac

Following the instructions from

  1. Download
  2. Run installer

if you don't set a $GOPATH, the default path is ~/go

Go Modules

Basic Commands

Remember to work outside of your $GOPATH!

  • initialize a new module

    export GO111MODULE=on
    go mod init
    • this will create a go.mod file in your current directory.
  • add a new dependency

    • in your .go file

    • build your code

      go build
    • check your go.mod file, it will only show the module

    • check transitive dependencies with

      go list -m all
  • list versions

    go list -m -versions
  • upgrade modules

    • check available update for all modules via

      go list -m -u all
    • update one specific package via (will update your dependency in go.mod)

      go get -u v0.3.0
    • upgrade all modules

      might not be a good idea though!

      go get -u
  • downgrading

  • use local changes

    # assume the local copy of your module is in ../quote
    go mod edit -replace ''
  • use remote fork / specific tag

    go mod edit -replace ''
  • testing

    • all modules involved in your release (test all modules plus transitive dependencies)

      go test -short all
    • test a single module package

      go test

Convert an existing Project

Working outside of the $GOPATH

  • Initialize Go modules inside the project directory

    go mod init
    go mod tidy

References for Go mdules

Language Details

Go Dependency Management

Testing with Go

Learning Go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment