Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Go Cheat Sheet

Go Cheat Sheet

Install Go on Mac

Following the instructions from https://golang.org/doc/install

  1. Download https://golang.org/doc/install?download=go1.13.3.darwin-amd64.pkg
  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

      import rsc.io/quote
    • build your code

      go build
    • check your go.mod file, it will only show the rsc.io/quote module

    • check transitive dependencies with

      go list -m all
  • list versions

    go list -m -versions rsc.io/sampler
  • 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 golang.org/x/text 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 'rsc.io/quote=../quote'
  • use remote fork / specific tag

    go mod edit -replace 'rsc.io/quote=github.com/myitcv/london-gophers-quote-fork@0.0.0-myfork'
  • testing

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

      go test -short all
    • test a single module package

      go test rsc.io/quote/...

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
You can’t perform that action at this time.