Skip to content

Instantly share code, notes, and snippets.

@brutella
Created December 7, 2018 12:57
Show Gist options
  • Save brutella/74cb81d197caa1cb62101292a550c2db to your computer and use it in GitHub Desktop.
Save brutella/74cb81d197caa1cb62101292a550c2db to your computer and use it in GitHub Desktop.
Go modules

Go supports modules since v1.11+.

Go modules require that GOPATH is empty => unset GOTPATH

Define a module

Run go mod init {module name} to create a go.mod file for the current directory.

For example go mod init github.com/brutella/dnssd creates the following content.

# go.mod
module github.com/brutella/dnssd

require (
	github.com/miekg/dns v1.1.1
	golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9 // indirect
	golang.org/x/net v0.0.0-20181201002055-351d144fa1fc
	golang.org/x/sys v0.0.0-20181206074257-70b957f3b65e // indirect
)

Run go get -v to fetch the modules defined in go.mod. Building the project automatically fetches the dependencies.

You can replace a dependency by using the replace keyboard like so

# go.mod
…
replace github.com/miekg/dns v1.1.1 => github.com/brutella/dns v1.0.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment