Golang another unrecognized import path - go

When i am trying to install golint (or gin, for example) i get "unrecognized import path error".
I know that there are many same questions, but main answer is to check environment variables.
There is screenshot of my environment variables, my folders and console with error.
I tried to install go both with .msi installer and just by copying files and setting env var manually. I got the same results.

There is a go get issue currently discussed
my go tools were out of date, but go get could not update them because they switched form mercurial to git at some point. Deleting the whole golang.org/x/tools directory in my GOPATH and reinstalling fixed the issue.
Make sure you have the latest go, and try with a fresh empty GOPATH folder, to see if the issue persists.

Related

Installing gota package in go workspace

I'm writing this away from my code so fingers crossed.
I've recently started learning Go from a Python background. I've set up my workspace (Linux Mint OS) so:
GOPATH=$HOME/go
GOROOT=/usr/local/go
Where under $HOME i have a dir called go and 3 subdirs called src, bin and pkg.
I wanted to mess around with some dataframes (I use pandas a lot in Python) so I tried to install gota from github. Only their recommended install command:
go get -u github.com/kniren/gota/dataframe
go get -u github.com/kniren/gota/series
returns an error saying it could find the package in GOPATH or GOROOT. To me this is strange as go get seems like the equivalent to pip install and shouldn't be looking in my path but rather at the URL. I managed to get some files to install but using goget and the github URL of the project:
go get github.com/go-gota/gota/dataframe
go get github.com/go-gota/gota/series
and this built some files under a gonum.org directory in my src dir and a linux_amd64 dir in my pkg directory. So far neither section have the expected path to the libraries and I can't find a suitable method to import. import statements look in gopath's src directory however I assume it should be looking in the pkg directory? Why is this and what's wrong with my env?
The authors of the repository must have migrated to a different repository.
The official repository of these packages is: github.com/go-gota/gota
https://www.github.com/kniren/gota/dataframe
https://www.github.com/kniren/gota/series
These repositories do not exist, this is why your go get commands failed. In fact, trying to navigate to github.com/kniren/gota redirects me to their official repository.

Error: code in directory ... expects import "..." - what do I do?

I have project in Go. After I set up debugger and launch in Visual Code,
it failed with this error output. I am using go 1.13.4 in /usr/local/go
/usr/lib/go-1.10/src/crypto/tls/cipher_suites.go:18:2: code in directory /usr/lib/go-1.10/src/vendor/golang_org/x/crypto/chacha20poly1305 expects import "golang.org/x/crypto/chacha20poly1305"
/usr/lib/go-1.10/src/crypto/x509/x509.go:36:2: code in directory /usr/lib/go-1.10/src/vendor/golang_org/x/crypto/cryptobyte expects import "golang.org/x/crypto/cryptobyte"
/usr/lib/go-1.10/src/crypto/x509/x509.go:37:2: code in directory /usr/lib/go-1.10/src/vendor/golang_org/x/crypto/cryptobyte/asn1 expects import "golang.org/x/crypto/cryptobyte/asn1"
/usr/lib/go-1.10/src/crypto/tls/key_agreement.go:20:2: code in directory /usr/lib/go-1.10/src/vendor/golang_org/x/crypto/curve25519 expects import "golang.org/x/crypto/curve25519"
/usr/lib/go-1.10/src/net/http/h2_bundle.go:48:2: code in directory /usr/lib/go-1.10/src/vendor/golang_org/x/net/idna expects import "golang_org/x/text/internal/export/idna"
/usr/lib/go-1.10/src/net/http/transport.go:32:2: code in directory /usr/lib/go-1.10/src/vendor/golang_org/x/net/proxy expects import "golang.org/x/net/proxy"
This seems to happen when you have a newer version of Go available on your PATH.
I worked around it by uninstalling Go 1.13 from /usr/local, but there may be better ways to handle it if you need to have multiple versions installed.
I had the VSCode Go debugger working with go.goroot set to a Go 1.11 install I was using for my project. I installed Go 1.13 into /usr/local to test out a feature and then ran into this same error.
If you have a different Go version on the PATH it seems like it's using that compiler, but compiling against the standard library sources in your other Go version. In later Go versions this directory has been renamed back to vendor/golang.org, and it doesn't recognize the golang_org hack that was used previously:
https://github.com/golang/go/commit/4d00937cecdea85b6f1eb894a6d28a53f5f2ff8a#diff-44c7c5a1dcc556d22e115d30ec0f11c9
I'm not certain if this is a general problem with Delve, or an issue with how the VSCode Go extension calls it, but the simplest solution in my situation was to uninstall Go 1.13 and the debugger started working again.
Your problem has nothing to do with the debugger. If you would have searched for the key parts of these errors, you would have discovered that the error is due to the fact since v1.4 Go supports so-called "canonical imports".
Most probably some packages in your project were vendored using their "hosting" URLs (such as github.com/what/ever) instead of their canonical URLs suggested by the error message.
The proper solution is to revendor the affecting packages the correct way.
If you merely want the code to build "as is" no matter what, you could edit the code of these packages to remove "canonical import comments" from them.

vscode does not detect errors golang

My vscode does not detect errors in golang.
Example:
package somepackage
import "fmt"
func f(name string) string {
name = 1
return name
}
This should throw a type error, but it does not. I never get any errors.
My settings.json contains
"go.gopath": "some/path",
"go.vetOnSave": "package",
"go.lintOnSave": "package",
"go.testOnSave": true,
"go.buildOnSave": "package",
"go.coverOnSave": true
I was able to run go: install/update Tools. All tools are installed successfully.
I am also able to run debug a .go file in vscode.
As #pwaterz pointed out, the solution to my problem was to add "go.goroot: /some/other/path".
The reason that vscode was not able to detect errors was, that there are different go versions on my computer. Adding the goroot and running go: install/update Tools solved the problem.
---- Edit: Multiple go versions ----
Had multiple conflicting go versions on my Mac, pulled in via brew. Fixed the problem with a reinstall.
Uninstall go and also run brew uninstall go
Reinstall go
Set environment variables in your .bash_profile or similar. Compare here.
Apply the changes to your profile by running e.g. source .bash_profile
Restart VSCode
In settings.json set "go.goroot": "/usr/local/go"
Run go: Toggle workspace trust space to make sure changes to settings.json are applied (you have to trust your workspace for that)
go: Install/update tools and select all
---- Edit: Incorrect root folder ----
Make sure that you open the root folder of your project and not a sub-folder of your project. That may cause in invalid import paths otherwise
---- Edit: Broken language server ----
Try to run go: restart language server
You may see Error loading workspace
run go mod tidy and try again

Issues installing a go program

Im new to go and I have been unable to find any thing online for my issue.
I have downloaded this code https://github.com/hashicorp/http-echo and I would like to set it up so I can run this command.
$ http-echo -listen=:8080 -text="hello world"
I have been getting quite a few different path issues.
Currently I have the code sitting in this directory.
/Users/jon/go/src/github.com/hashicorp
When I try and install it I get this error
$ go install http-echo
can't load package: /usr/local/go/src/http-echo/handlers.go:9:2: non-standard import "github.com/hashicorp/http-echo/version" in standard package "http-echo"
Where should I keep go projects on an OSX computer, and how do I get this to install or compile?
The code currently seems to be in /usr/local/go/src/http-echo. Packages should always reside in the directory $GOPATH/src/package-name, e.g.: $GOPATH/src/github.com/hashicorp/http-echo. (unless you're using go modules).
It should work if you move the source to the correct path (/Users/jon/go/src/github.com/hashicorp/http-echo). Then execute:
go install github.com/hashicorp/http-echo
Even easier would be to use go get to download the package in the first place. Simply run the following command from any directory:
go get github.com/hashicorp/http-echo
And http-echo is automagically installed.
If you still get an error after this, make sure $GOPATH/bin is in your $PATH.

import object is expected between different go versions

I switched from go 1.6 to 1.4.2 for faster compilation. When I recompile the earlier compiled binary of 1.6, I get the following error:
import go/pkg/darwin_amd64/github.com/Sirupsen/logrus.a: object is
[darwin amd64 go1.6 X:none] expected [darwin amd64 go1.4.2 X:precisestack]
I have already tried go build -a - which works without any error. And have also tried go install -a and go build - both of these commands throw the above mentioned error.
I already know that, if I delete by go/pkg folder, then it will work. That is the answer of the already existing SO question
I wanted to know, if there is another way to resolve this using go tools, other than deleting the pkg folder.
Most likely due to previous compiles. Just delete your $GOLANG/pkg and you are good to go!
another way to resolve this using go tools, other than deleting the pkg folder.
The go tool chain is unlikely to be modified to handle this corner use case.

Resources