What does it mean the code in my directory "expects" a particular import?

Viewed 2008

A Go language tutorial uses the "quote" package to get familiar quotes. When I give the command "go get -v rsc.io/quote", I get the message

code in directory.../github/hello/src/rsc.io/quote/v3 expects import "rsc.io/quote"

So I went there and looked at the code. It has an import statement:

import "rsc.io/quote/v3"

So I thought maybe I had the wrong version of quote. Accordingly, I tried "go get -v rsc.io/quite/v3" THis produces the message:

code in directory .../rsc.io/quote/v3 expects import "rsc.io/quote."

This seems circular: asking without a version number produces a requirement for a versioned package, but trying to get a versioned package produces a requirement without a version number.

3 Answers

If you continue the tutorial to the end you will see that the addition of a go module, and the subsequent initialisation, will resolve your issue. The .mod file will pull in the required dependencies.

Can you try

go get -v rsc.io/quote

get "rsc.io/quote": found meta tag get.metaImport{Prefix:"rsc.io/quote", VCS:"git", RepoRoot:"https://github.com/rsc/quote"} at //rsc.io/quote?go-get=1
rsc.io/quote (download)
get "rsc.io/sampler": found meta tag get.metaImport{Prefix:"rsc.io/sampler", VCS:"git", RepoRoot:"https://github.com/rsc/sampler"} at //rsc.io/sampler?go-get=1
rsc.io/sampler (download)
github/gospace/src/rsc.io/quote/quote.go:8:8: code in directory /Users/james/github/gospace/src/rsc.io/quote/v3 expects import "rsc.io/quote"

cat $GOPATH/src/rsc.io/quote/v3/go.mod
module rsc.io/quote/v3

require rsc.io/sampler v1.3.0
  ~> cat $GOPATH/src/rsc.io/quote/v3/go.sum
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c h1:qgOY6WgZOaTkIIMiVjBQcw93ERBE4m30iBm00nkL0i8=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
rsc.io/sampler v1.99.99 h1:7i08f/p5TBU5joCPW3GjWG1ZFCmr28ybGqlXtelhEK8=
rsc.io/sampler v1.99.99/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=

More about go get vs go install. What is the difference between go get and go install?

Can you please check your GO env path settings:

export GOPATH=$HOME/github/<your_workspace_name>                                                   
export GOBIN=$GOPATH/bin
export GOROOT=/usr/local/go
export PATH=$PATH:/usr/local/bin:/usr/local/go/bin:$GOPATH/bin

Here is a good source for setting up workspace:

https://golang.org/doc/gopath_code.html

As @MartinBennett mentioned, it's already mentioned in the tutorial but may be not so clear.

Once you add import "rsc.io/quote" in your hello.go file, you will need to run these commands in your terminal:

1-

go mod init hello

2-

go run hello.go
Related