Go install: “Can't load package” (even though GOPATH is set) - go

I'm just getting started with the Go programming language and installed Go using the Windows installer from the website. I tested installation by using go run hello.go and that works. The problem comes when I try to build my first program:
$ echo $GOROOT
C:\Go\
$ echo $GOPATH
/cygdrive/c/Users/Paul/Documents/Home/go
mkdir -p $GOPATH/src/hello
Inside that directory I have a simple hello.go program:
package main
import "fmt"
func main() {
fmt.Printf("Hello, world.\n")
}
The problem comes when I try to build and install:
$ go install hello
can't load package: package hello: cannot find package "hello" in any of:
C:\Go\src\hello (from $GOROOT)
\cygdrive\c\Users\Paul\Documents\Home\go\src\hello (from $GOPATH)

GOPATH environment variable must contain valid path.
\cygdrive\c\Users\Paul\Documents\Home\go\src\hello is not a valid path on Windows.
Try setting GOPATH=c:\Users\Paul\Documents\Home\go instead.

Related

How should I resolve this import problem with gorilla/mux?

could not import github.com/gorilla/mux (cannot find package "github.com/gorilla/mux" in any of
C:\Program Files\Go\src\github.com\gorilla\mux (from $GOROOT)
C:\Users\lenovo\go\src\github.com\gorilla\mux (from $GOPATH))compilerBrokenImport
I have installed gorilla mux using the cmd "go get github.com/gorilla/mux" but I'm getting this error.enter image description here
Looks like you didn't run "go mod init" before the "go get".
In cmd, try this:
cd [the dir of your source code]
go mod init
go get github.com/gorilla/mux
For more information run:
go help mod
Afterwards, you might also need to restart VS Code.

cannot find package "bufio" in any of

I have get go1.11.5 installed by downloading precompiled binary package directly on my ubuntu18.04. Now i want to install go1.12 by building from source, so i follow with Installing Go from source.
I set GOROOT_BOOTSTRAP=~/goroot_bootstrap
$ mkdir -p ~/goroot_bootstrap/bin
$ cp /usr/local/go/bin/go ~/goroot_bootstrap/bin/
$ echo "export GOROOT_BOOTSTRAP=~/goroot_bootstrap" >> ~/.bashrc
$ source ~/.bashrc
Then, get source
$ git clone -b release-branch.go1.12 https://github.com/golang/go.git ~/github.com/golang/go
Build
$ cd ~/github.com/golang/go/src
$ ./all.bash
It failed, and tips:
Building Go cmd/dist using /home/pi/goroot_bootstrap.
cmd/dist/imports.go:12.2: cannot find package "bufio" in any of:
/home/pi/goroot_bootstrap/src/bufio (from $GOROOT)
/home/pi/go/src/bufio (from $GOPATH)
...
But if i set GOROOT_BOOTSTRAP=/usr/local/go, it will success.
Then, i read source code at src/make.bash.
166 rm -f cmd/dist/dist
167 GOROOT="$GOROOT_BOOTSTRAP" GOOS="" GOARCH="" GO111MODULE=off "$GOROOT_BOOTSTRAP/bin/go" build -o cmd/dist/dist ./cmd/dist
The bootstrap toolchains build cmd/dist with GOROOT="$GOROOT_BOOTSTRAP" at line 167.
If i have GOROOT_BOOTSTRAP=/usr/local/go set and success, which means building still depending old package such as bufio.
So, why it failed when setting GOROOT_BOOTSTRAP=~/goroot_bootstrap?
Do building depends old package if setting GOROOT_BOOTSTRAP=/usr/local/go?
Which one is the right method to install from source?
Any help will be grateful.

go test foo - cannot find package foo

I have this directory structure:
https://github.com/netjet-chrome-extension/netjet-mono/tree/master/examples/projects/golang
I try to run test.sh, which consists of:
#!/usr/bin/env bash
cd "$(dirname "$BASH_SOURCE")"
export GOPATH="$PWD"
go test sourcegraph_go_selenium
but I get this error:
can't load package: package sourcegraph_go_selenium: cannot find
package "sourcegraph_go_selenium" in any of:
/usr/lib/go-1.10/src/sourcegraph_go_selenium (from $GOROOT)
/home/oleg/codes/netjet/netjet-mono/examples/projects/golang/src/sourcegraph_go_selenium
(from $GOPATH)
GOPATH is set correctly, so why can't it find the sourcegraph_go_selenium package? this package is right there, in src/sourcegraph_go_selenium...?
Try this:
go test sourcegraph_go_selenium/...

Cannot find package under GOPATH

I am trying to install the dependencies of my project with glidebut unfortunately it fails with the following message:
main.go:7:2: cannot find package "github.com/arschles/go-in-5-minutes/episode13/models" in any of:
/Users/theo/go-workspace/src/github.com/thitami/go-in-5-minutes/episode13/vendor/github.com/arschles/go-in-5-minutes/episode13/models (vendor tree)
/usr/local/Cellar/go/1.8.3/libexec/src/github.com/arschles/go-in-5-minutes/episode13/models (from $GOROOT)
/Users/theo/go-workspace/src/github.com/arschles/go-in-5-minutes/episode13/models (from $GOPATH)
Running a go env, this is my env variables of interest:
GOPATH="/Users/theo/go-workspace"
GOROOT="/usr/local/Cellar/go/1.8.3/libexec"
Please be advised that I am zsh and I am exporting the GOPATH inside the .zshrc file like this:
export GOPATH=HOME/go-workspace
Any ideas are appreciated
* UPDATE *
As requested this is the piece of code with the call to the models package:
import (
"database/sql"
"log"
"github.com/arschles/go-in-5-minutes/episode13/models"
_ "github.com/mxk/go-sqlite/sqlite3"
)
You can only set the gopath once and you'll have to reset it every time you change packages. Think of it as a virtualenv. One way around it is to install the packages global or by using something like gvm

"cannot find package" error on godoc playable examples

I'm trying to make palyable example on godoc.
OS X Yosemite 10.10.5
Go: go1.7.4 darwin/amd64
test code:
$GOPATH/src/hoge/hoge_test.go
package hoge_test
import (
"fmt"
"hoge"
"testing"
)
func ExampleHoge() {
fmt.Println(hoge.Hoge())
// Output:
// hoge!!
}
The test passes:
$ go test hoge
ok hoge 0.011s
Godoc
$ godoc -play -http=:8080
I can see the hoge package's example playground as I expected on a web browser, but an error below occurs when I 'Run' the example.
tmp/sandbox389904218/main.go:5:2: cannot find package "hoge" in any of:
/usr/local/go/src/hoge (from $GOROOT)
/go/src/hoge (from $GOPATH)
I set my own GOPATH, but it's not /go as shown in the error.
What I need to run examples.
Additional
I've got a reason of the error above.
Godoc playable example redirects to play.golang.org to run the code, so the GOPATH in the error seems to be on the play.golang.org environment.
I'm still not sure how to run my own pkg example though...
This is because godoc uses the golang.org/x/tools/playground tool, which just proxies requests to golang.org. It does not appear that you can run your example functions unless they are in a package that is published on golang.org. Here's the relevant file in github: https://github.com/golang/tools/blob/master/playground/common.go which is referenced by the godoc tool here: https://github.com/golang/tools/blob/master/cmd/godoc/play.go

Resources