In Go, when I import a dependency like:
import "github.com/spf13/viper"
Which directories will Go look into, and in which order?
Lookup order:
The vendor folder.
The standard packages.
GOPATH folder.
Run go help gopath to learn more on how Go search each directory.
A quick and easy way to see the search order and exact paths searched n a particular system is to run go build err.
This produces output similar to this:
can't load package: package err: cannot find package "err" in any of:
C:\go\src\err (from $GOROOT)
\\FREENAS\Global Documents\Projects\Go\src\err (from $GOPATH)
Related
I'm trying to follow the Writing an intepreter in Go book, by Thorsten Ball, and in the first chapter he establish this simple scheme
file /Users/myuser/projects/monkey/token/token.go
file /Users/myuser/projects/monkey/lexer/lexer.go
file /Users/myuser/projects/monkey/lexer/lexer_test.go
In lexer/lexer.go and lexer/lexer_test.g the files start as
package lexer
And in lexer_test.go the imports are
import (
"testing"
"monkey/token"
)
Then he says that for running the test, I have to run (from /Users/myuser/projects/monkey directory):
go test lexer/lexer_test.go
But when I do this, I receive the error:
lexer/lexer_test.go:6:2: cannot find package "monkey/token" in any of:
/usr/local/opt/go/libexec/src/monkey/token (from $GOROOT)
/Users/myuser/golang/src/monkey/token (from $GOPATH)
FAIL command-line-arguments [setup failed]
FAIL
I've been trying to understand how to configure the packages in go, but I found a lot of very complicated documentation about GOPATH, GOROOT and go.mod. I've been trying all this approach without get rid of the issue.
Can someone help me please? I'm sure is a simple fix but I cannot figure it out :(
As the error message says, the compiler couldn't find the package locally.
Are you sure you have installed the package?
You may need to do go get [packagename]
For e.g., go get golang.org/x/tools/cmd/goimports
For Golang, It will find package first from go root then go path then import from outside. So basically, you should have that package monkey/token in your go root or go path.
I don't think it is about goimport because monkey/token didn't seem to be official lib that can be import.
When I first try Golang I have the same problem. I solve by create that package in go path (your working dir) or create that package from your book.
I'm trying to build a api using Go and I tried running a existing file from GitHub that I got from udemy course. I'm getting this kind of output:
C:\goworkspace\src\grpc-go-course-master\blog\blog_client>go run client.go
client.go:4:2: cannot find package "context" in any of:
C:\go\src\context (from $GOROOT)
C:\goworkspace\src\context (from $GOPATH)
..\..\..\golang.org\x\net\http2\not_go111.go:10:2: cannot find package "net/http/httptrace" in any of:
C:\go\src\net\http\httptrace (from $GOROOT)
C:\goworkspace\src\net\http\httptrace (from $GOPATH)
Could you suggest me what can cause this error?
In order to download the dependencies, you have to use the go get command:
go get -v -u github.com/alessiosavi/GoUtils
Change with the package that you need (in that repo you can find a bunch of 'utils' method).
If the project, instead, have the go.mod file, you have to type:
go clean
go build
By this way you are going to install the dependencies listed in the go.mod file.
For update the dependencies to the latest version:
go get -v -u all
go mod tidy
The above command looks for missing dependencies, adds them to go.mod, and installs them.
I'm in my go root path and I get error the following errors when I perform the go install command...
main.go:4:2: cannot find package "fmt" in any of:
/usr/lib/go-1.6/bin/src/fmt (from $GOROOT)
/home/rbtlong/go/src/fmt (from $GOPATH)
main.go:6:2: cannot find package "io/ioutil" in any of:
/usr/lib/go-1.6/bin/src/io/ioutil (from $GOROOT)
/home/rbtlong/go/src/io/ioutil (from $GOPATH)
main.go:5:2: cannot find package "net/http" in any of:
/usr/lib/go-1.6/bin/src/net/http (from $GOROOT)
/home/rbtlong/go/src/net/http (from $GOPATH)
package rbtlong.com
imports runtime: cannot find package "runtime" in any of:
/usr/lib/go-1.6/bin/src/runtime (from $GOROOT)
/home/rbtlong/go/src/runtime (from $GOPATH)
I have tried unsetting the GOROOT, but it doesn't seem to work.
Per discussion, it sounds like your install is a bit mucked up. Reinstalling Go seems to be the best option.
For others investigating GOROOT issues, the first step is almost always going to be to unset GOROOT. In most cases, GOROOT is not needed, and tends to muck up the works.
Another option for managing your Go installs more easily (and largely automagically), which also gives a homebrew-like version swapping capability and a containerization similar to virtualenv for Python or rvm for Ruby, is to use GVM.
apparently, the definition was defined in $HOME/.bashrc after removing the definition for GOPATH and GOROOT, then manually removing the folder at /usr/lib/go-1.6 then reinstalling it using the instructions here https://golang.org/doc/install#install then setting the PATH and GOPATH by editing $HOME/.profile:
PATH="[...]:/usr/local/go/bin[...]"
export GOPATH="$HOME/go"
it appears to be working now...
Before doing go install check for the Go version and Go path, if these are correct you won't get any error.
Please, install Go latest version from the official website and re-try once.
go:5:2: cannot find package "github.com/googollee/go-socket.io" in any of:
/usr/local/go/src/github.com/googollee/go-socket.io (from $GOROOT)
/Users/YoungHoonKim/Documents/chat/src/github.com/googollee/go-socket.io (from $GOPATH)
I'm getting this error while trying to compile my chat.go file with LiteIDE. I'm following a guide to build a chatserver, so I don't know where this package should be. any help?
Double-check your actual go work directory with echo $GOPATH in your terminal.
This will be your starting point to identify if you have this package at all, for example you should be able to see this package doing cd $GOPATH/src/github.com/googollee/go-socket.io.
For more details on correct directory location (workspace in general) please check https://golang.org/doc/code.html
This is my first time asking a question here, anyway, I am wondering why it says it can't find the package, even though github.com/JohnDoe/temperature/conversion/conv is inside the $GOPATH
[johndoe#johndoe temperature]$ go run main.go
main.go:5:2: cannot find package "github.com/JohnDoe/temperature/conversion/conv" in any of:
/usr/local/go/src/github.com/JohnDoe/temperature/conversion/conv (from $GOROOT)
/home/johndoe/Workspace/Go/src/github.com/JohnDoe/temperature/conversion/conv (from $GOPATH)
more details:
[johndoe#johndoe conversion]$ pwd
/home/johndoe/Workspace/Go/src/github.com/JohnDoe/temperature/conversion
[johndoe#johndoe conversion]$ ls
conv.go
Thoughts?
You're mistaking packages and file. In go (contrary to say… python), a package is represented by a directory, and files in this directory are nothing more than code. So you should import the github.com/JohnDoe/temperature/conversion package.