Skip to content

Instantly share code, notes, and snippets.

@wallyqs
Last active February 22, 2020 04:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wallyqs/7dfe08e6833b2075b5a947bb986949ab to your computer and use it in GitHub Desktop.
Save wallyqs/7dfe08e6833b2075b5a947bb986949ab to your computer and use it in GitHub Desktop.
NATS v2 & Go Modules

In case having issues during when using the recent versions of NATS such as:

go: github.com/nats-io/go-nats@v1.8.1: parsing go.mod: unexpected module path "github.com/nats-io/nats.go"
go: github.com/nats-io/go-nats-streaming@v0.5.0: parsing go.mod: unexpected module path "github.com/nats-io/stan.go"

To fix it:

  1. Update your go.mod using the latest tags, for example for both NATS and NATS Streaming clients:
module github.com/wallyqs/hello-nats-go-mod

go 1.12

require (
	github.com/nats-io/nats.go v1.8.1
	github.com/nats-io/stan.go v0.5.0
)

Or if want to import the NATS Server v2 to embed it, notice the /v2 after the nats-server module name. If that is not present, then go modules will not fetch it and would accidentally end up with 1.4.1 version of the server.

require (
	github.com/nats-io/nats-server/v2 v2.0.0
	github.com/nats-io/nats.go v1.8.1
)

If embedding both NATS Streaming and NATS Servers:

require (
	github.com/nats-io/nats-server/v2 v2.0.0 // indirect
	github.com/nats-io/nats-streaming-server v0.15.1
)	
  1. Next, update the imports within the repo:
find ./ -type f -name "*.go" -exec sed -i -e 's/github.com\/nats-io\/go-nats-streaming/github.com\/nats-io\/stan.go/g' {} \;

find ./ -type f -name "*.go" -exec sed -i -e 's/github.com\/nats-io\/go-nats/github.com\/nats-io\/nats.go/g' {} \;

find ./ -type f -name "*.go" -exec sed -i -e 's/github.com\/nats-io\/gnatsd/github.com\/nats-io\/nats-server\/v2/g' {} \;

find ./ -type f -name "*.go" -exec sed -i -e 's/github.com\/nats-io\/nats-server/github.com\/nats-io\/nats-server\/v2/g' {} \;
  1. (Recommended) Run Go fmt as that the rename will affect the proper ordering of the imports

Gotchas when using go get

If using go get for the client, then have to be careful with adding an extra slash at the end of the repo for example:

GO111MODULE=on go get github.com/nats-io/nats.go/@latest
GO111MODULE=on go get github.com/nats-io/nats.go/@v1.8.1

If trying to fetch the latest version of the server with go get, then have to add v2 at the end:

GO111MODULE=on go get github.com/nats-io/nats-server/v2@latest

Otherwise, go get will fetch the v1.4.1 version of the server, which is also named (gnatsd).

GO111MODULE=on go get github.com/nats-io/nats-server@latest
go: finding github.com/nats-io/gnatsd/server latest
go: finding golang.org/x/crypto/bcrypt latest
go: finding golang.org/x/crypto latest

In order to use an older tag, also have to use the old name otherwise it would results in go mod parsing errors.

# OK
GO111MODULE=on go get github.com/nats-io/go-nats/@v1.7.2

# Not OK
GO111MODULE=on go get github.com/nats-io/nats.go/@v1.7.2
go: finding github.com/nats-io/nats.go v1.7.2
go: downloading github.com/nats-io/nats.go v1.7.2
go: extracting github.com/nats-io/nats.go v1.7.2
go: finding github.com/nats-io/go-nats/encoders/builtin latest
go: finding github.com/nats-io/go-nats/util latest
go: finding github.com/nats-io/go-nats/encoders latest
go: finding github.com/nats-io/go-nats v1.8.1
go: downloading github.com/nats-io/go-nats v1.8.1
go: extracting github.com/nats-io/go-nats v1.8.1
go: github.com/nats-io/go-nats@v1.8.1: parsing go.mod: unexpected module path "github.com/nats-io/nats.go"
go: error loading module requirements
@danf0rth
Copy link

@wallyqs, now when i do go get -u ./... there is no errors, so it seems like the problem is solved. I am not sure what exactly solves the problem. Thanks for your help! NATS is awesome.

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