Cannot find package within project directory - go

I've been trying to solve this problem for the past two hours. I cannot, for the life of me, understand how the folks who developed Go did such a fine job with the language but such a terrible job with package management.
Here is my go env output:
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/<me>/Library/Caches/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/<me>/Documents/Proj/go"
GORACE=""
GOROOT="/usr/local/opt/go/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/opt/go/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/z4/b3lxy_51405_b8pb_680l4xh0000gn/T/go-build063693521=/tmp/go-build -gno-record-gcc-switches -fno-common"
Notice that my GOPATH and GOROOT are set.
Here is the structure of my go workspace (/Users/<me>/Documents/Proj/go):
go/
|-bin/
|-pkg/
|-src/
|--github.com/
|--user/
|--myproject/
|--client/
|--client.go
|--main.go
Here is my client.go file:
// client.go
package client
type MyClient struct {}
this is my main.go file:
// main.go
package main
import "client"
func main() {}
When I run go build in the project root, I get this error:
main.go:4:2: cannot find package "client" in any of:
/usr/local/opt/go/libexec/src/transport (from $GOROOT)
/Users/<me>/Documents/Proj/go/src/transport (from $GOPATH)
The first time this happened, I tried to correct the path above:
// main.go
package main
import "github.com/user/myproject/client"
func main() {}
Then I got this error:
main.go:4:2: cannot find package "client" in any of:
/usr/local/opt/go/libexec/src/transport (from $GOROOT)
/Users/<me>/Documents/Proj/go/src/github.com/user/myproject/transport (from $GOPATH)
Which is bizarre, since the package exists at that exact path.
What am I doing wrong??

You should not import client but which path is coming from GOPATH/src, in this particular case github.com/user/myproject/client because that is your path.
I see that the way you describe your problem is not exactly the way it is on your computer. You replaced some things. That is understandable, you do not want to tell the world how your computer looks like. So you replaced your name with "<me>".
You also replaced the projectname, with MyProject in your question, that is also, just like the user name, a potential spot for an error which we cannot see, because you replaced it. Never use long names in development-paths on Windows, because Windows is doing dirty tricks with them, sometimes.
I am sure there is something the matter which is a problem. Try using your short-directory name in the go-path. Check this: https://superuser.com/questions/1182040/strange-short-path-name-on-windows

Related

How I can fix the trouble with go build and go install doesnt work [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last year.
Improve this question
Hello guys decided to download one tool - https://github.com/Charlie-belmer/nosqli (wrote on GO) for my pentest practice (it's made the nosqli directory in my /home/user).
And found out that it's doesn't work. So I've started to fix this problem and stucked:
when I did "go install" it did nothing I mean literally without error msg etc. Now it gaves me that:
go install main.go:19:8: cannot find package "github.com/Charlie-belmer/nosqli/cmd" in any of: /usr/lib/go-1.17/src/github.com/Charlie-belmer/nosqli/cmd (from $GOROOT) /root/go/src/github.com/Charlie-belmer/nosqli/cmd (from $GOPATH)
And same situation with go build.
**# go version
go version go1.17.6 linux/amd64**
**go env** (output):
GO111MODULE="off"
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/root/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go-1.17"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go-1.17/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.17.6"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3159993699=/tmp/go-build -gno-record-gcc-switches"
I've never used Go before and absolutely stucked, tried to read so guids but resultless.
How did you download the tool? Did you clone the repo from Github and trying to build it? If you just want to use the tool why not grab binary release for your OS here: https://github.com/Charlie-belmer/nosqli/releases/tag/v0.5.4 ?
If you really want to build it, you will need to clone the repository:
git clone https://github.com/Charlie-belmer/nosqli.git /some/dir
and run
cd /some/dir
go build -o nosqli .
go tool will fetch all dependencies as part of process and build the binary (named as whatever comes after -o - in this case nosqli), no need to run go install at all. nosqli binary should then be in /some/dir after downloading dependencies and build finishes.
Normally you would install a package that you want to use in the software you're making.
Let's assume you want to use some package.
Then the command will be
go install the_complete_package
example:
go install github.com/marcelloh/gotest#latest

linker command failing for particular golang test involving "net/http" and "time" packages

I'm new to web development and golang. I'm trying to learn go from here. Without any issues I was able to learn till concurrency part, however, I'm facing issues while trying to learn select statements. Here is what I did. hello.go contains the function, while hello_test.go has the testing code. This is the hello.go code.
package lol
func Racer(a, b string) (winner string) {
return
}
Below is the testing code
package lol
import "testing"
func TestRacer(t *testing.T) {
slowURL := "http://www.facebook.com"
fastURL := "http://www.quii.co.uk"
want := fastURL
got := Racer(slowURL, fastURL)
if got != want {
t.Errorf("got %q, want %q", got, want)
}
}
The test is failing just as expected with message hello_test.go:13: got "", want "http://www.quii.co.uk". Now I'm trying to write some more code to hello.go to make it pass.
package lol
import(
"time"
"net/http"
)
func Racer(a, b string) (winner string) {
startA := time.Now()
http.Get(a)
aDuration := time.Since(startA)
startB := time.Now()
http.Get(b)
bDuration := time.Since(startB)
if aDuration < bDuration {
return a
}
return b
}
When I'm testing now, it is giving me # runtime/cgo ld: unsupported tapi file type '!tapi-tbd' in YAML file '/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib/libSystem.tbd' for architecture x86_64 clang-10: error: linker command failed with exit code 1 (use -v to see invocation) FAIL module [build failed]. After searching on internet forums on this error, I did sudo rm -rf /Library/Developer/CommandLineTools and reinstalled them back, but didn't help. I think it may be due to the modules I'm trying to import but I'm not sure. I don't know if it is relevant or not but I updated to macOS Big Sur two days before facing the issue. Please help in resolving the issue. TIA
Edit: go env is returning
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/{user_name}/Library/Caches/go-build"
GOENV="/Users/{user_name}/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/{user_name}/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/{user_name}/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.16.5/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.16.5/libexec/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.16.5"
GCCGO="gccgo"
AR="/Users/{user_name}/opt/anaconda3/bin/x86_64-apple-darwin13.4.0-ar"
CC="x86_64-apple-darwin13.4.0-clang"
CXX="x86_64-apple-darwin13.4.0-clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/zc/4r25vdyj4cv3ynjx64633ly40000gn/T/go-build3491450963=/tmp/go-build -gno-record-gcc-switches -fno-common"
Edit 2:
The test started working when I ran CGO_ENABLED=0 go test -v ./.... It returned the expected output.
=== RUN TestRacer
hello_test.go:13: got "http://www.facebook.com", want "http://www.quii.co.uk"
--- FAIL: TestRacer (2.52s)
FAIL
FAIL module 3.076s
FAIL
I'm still trying to understand why I need to add CGO_ENABLED=0 to for this particular test.
In my case it was R Studio installed through Anaconda package manager. After it was removed, the linker started working as expected.

How to deal with "import cycle not allowed" while trying to install gonum?

I'd like to use the gonum libraries for go in order to experiment with some neural network stuff but I cannot go past the install process...
I'm running the command found on the official gonum website :
go get -u -t gonum.org/v1/gonum/...
But it gives me :
import cycle not allowed
package gonum.org/v1/gonum
imports runtime
imports internal/bytealg
imports internal/cpu
imports runtime
Do you know what could be a reason for such a problem?
In case you need my go env in order to help me out, here it is :
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/me/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GO15VENDOREXPERIMENT="1"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"
I'm running go 1.6.2.
As Adrian told in the comments, the issue was that the go version I was running was too old for gonum to install correctly. This was due to the fact that the go-golang package installed on my computer via apt-get was giving me the 1.6 version of go. By removing the package and making sure I had a recent go release installed on my computer I managed to install gonum.

src/golang.org directory missing

I'm a newcomer to Go and I think I've messed up the setup somewhere. When trying to install the Buffalo framework, I got the following error:
$ buffalo new todo-api --api
Buffalo version v0.9.5
--> go get -u golang.org/x/tools/cmd/goimports
package golang.org/x/tools/cmd/goimports: cannot find package "golang.org/x/tools/cmd/goimports" in any of:
/usr/local/go/src/golang.org/x/tools/cmd/goimports (from $GOROOT)
/home/ankush/go/src/golang.org/x/tools/cmd/goimports (from $GOPATH)
I remember emptying the $HOME/go directory because I had just upgraded from 1.8 to 1.9, and thought that Go would somehow automatically re-download and populate it. But it didn't, it seems.
So, is the src/golang.org directory supposed to be in $GOPATH? Because I didn't find it in $GOROOT and I had followed the standard setup instructions.
Any suggestions?
== Update 1 ==
The GOPATH is correctly set in my case. Here's the output of go env:
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/ankush/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build630744714=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
=== Update2 ===
Here's my output of go get for goimports:
go$ go get -u -v golang.org/x/tools/cmd/goimports
Fetching https://golang.org/x/tools/cmd/goimports?go-get=1
Parsing meta tags from https://golang.org/x/tools/cmd/goimports?go-get=1 (status code 200)
get "golang.org/x/tools/cmd/goimports": found meta tag get.metaImport{Prefix:"golang.org/x/tools", VCS:"git", RepoRoot:"https://go.googlesource.com/tools"} at https://golang.org/x/tools/cmd/goimports?go-get=1
get "golang.org/x/tools/cmd/goimports": verifying non-authoritative meta tag
Fetching https://golang.org/x/tools?go-get=1
Parsing meta tags from https://golang.org/x/tools?go-get=1 (status code 200)
golang.org/x/tools (download)
package golang.org/x/tools/cmd/goimports: cannot find package "golang.org/x/tools/cmd/goimports" in any of:
/usr/local/go/src/golang.org/x/tools/cmd/goimports (from $GOROOT)
/home/ankush/go/src/golang.org/x/tools/cmd/goimports (from $GOPATH)
I was finally able to solve it. For someone else who gets bitten by this in future, in my case it helped to remove the concerned directory and then install again. Here's what helped:
$ rm -rf /home/ankush/go/src/golang.org/x/tools
$ go get -u -v golang.org/x/tools/cmd/goimports
I'm not sure why this works, but maybe the the git pull had somehow failed earlier (sloppy connection?) as soon as the directory was created.

Go package imports self, and can't find itself in it the GOPATH

Every once in awhile, I'll use a go package that has a go get like : go get github.com/<user>/aRepo and in its own import statement have import "github.com/<user>/aRepo". So it imports itself using a the "go gettable" path. Sometimes this works fine; however, sometimes I get
package github.com/<user>/aRepo
imports github.com/<user>/aRepo
imports github.com/<user>/aRepo: cannot find package "github.com/<user>/aRepo" in any of:
/usr/local/go/src/github.com/<user>/aRepo (from $GOROOT)
/home/me/go/src/github.com/<user>/aRepo (from $GOPATH)
I set my GOPATH with:
export GOPATH=$GOPATH:/home/me/go:`godep path`
It's very unclear as to why this would happen, any ideas as to why?
EDIT
my go env:
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH=":/home/vagrant/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"
So it turns out it was a permissions issue since I was using a vagrant box with a synced folder, vagrant set the entire path that synced folder created as 744 root:root. Therefore, I could not write to that folder as a non-root user, so that error message is actually coming from godep trying to install itself and not finding its src.

Resources