Go build: "Cannot find package" (although GOPATH is installed)

Although I installed GOPATH , I still cannot get "go build" or "go run" to find my own packages. What am I doing wrong?

 $ echo $GOROOT /usr/local/go $ echo $GOPATH /home/mitchell/go $ cat ~/main.go package main import "foobar" func main() { } $ cat /home/mitchell/go/src/foobar.go package foobar $ go build main.go main.go:3:8: import "foobar": cannot find package 
+88
build go package
Nov 03 '12 at 22:15
source share
4 answers

This does not work because your source file foobar.go not in a directory named foobar . go build and go install try to map directories, not source files.

  • Install $GOPATH in a valid directory, for example. export GOPATH="$HOME/go"
  • Move foobar.go to $GOPATH/src/foobar/foobar.go and the build should work fine.

Additional recommended steps:

  • Add $GOPATH/bin to your $PATH with: PATH="$GOPATH/bin:$PATH"
  • Move main.go to a subfolder of $GOPATH/src , for example. $GOPATH/src/test
  • go install test should now create an executable file in $GOPATH/bin , which can be called by typing test in your terminal.
+115
Nov 04 '12 at 22:26
source share

Edit: since you meant GOPATH, see fasmat answer (upvoted)

As mentioned in " How do I find my package ? , you need to put the xxx package in the xxx directory.

See Go to Language Specification :

 package math 

A set of files that have the same PackageName form for implementing the package.
An implementation may require that all source files for a package be in the same directory.

The code organization mentions:

When creating a program that imports the widget package, the go command searches for src/pkg/widget inside the Go root, and then - if the source of the package is not found there, it searches for src/widget inside each workspace in order.

("workspace" is a path entry in your GOPATH : this variable can refer to several paths for your " src, bin, pkg ")




(Original answer)

You should also set GOPATH to ~ / go, not GOROOT , as shown in " How to write a jump code ."

The Go path is used to enable import statements. It is implemented and documented in the go / build package.

The GOPATH environment GOPATH lists the places to search for Go code.
On Unix, a value is a colon delimited string.
On Windows, the value is a comma delimited string.
In Plan 9, a value is a list.

This is different from GOROOT :

Go binary distributions assume that they will be installed in /usr/local/go (or c:\Go under Windows), but they can be installed elsewhere.
If you do this, you will need to set the GOROOT environment GOROOT to this directory when using the Go tools.

+8
Nov 03 '12 at 22:30
source share

TL; DR: follow the rules of Go! (lesson learned the hard way), check for old go versions and delete them. Install the latest version.

For me, the decision was different. I was working on a shared Linux server, and after several GOPATH and other environment variables, it still didn't work. I encountered several errors, including "Unable to find package" and "Unrecognized import path". After trying to reinstall with this solution following the instructions on golang.org (including the uninstall part), problems still occurred.

It took me a while to realize that there is still an old version that has not been removed (launched by go version which go again which go ... DAHH), which gave me this question and finally solved it.

+1
Jan 31 '18 at 7:43
source share

Have you tried adding an absolute go directory to your "path"?

 export PATH=$PATH:/directory/to/go/ 
-one
Nov 04
source share



All Articles