Why would this particular package fail to be auto-imported? - go

I'm using VSCode with a few Go projects, in a workspace. I've got pretty standard VSCode settings, and I'm using the Go extension with gopls.
When in my code I'm using a package which has been installed in my current module, it is auto-imported successfully (i.e. "github.com/gosimple/slug").
But when I'm using spew the auto-import happens to be "app/env/pkg/mod/github.com/davecgh/go-spew#v1.1.1/spew" instead of "github.com/davecgh/go-spew/spew", everytime.
It also happens (only sometimes) with another package, "github.com/google/uuid".
Now I don't think it's related to these particular packages in any way, but I'm struggling to find a cause to this, and why only these packages.
vscode 1.68.1
go 1.18
gopls 0.9.0

Related

Is there an alternative to get the Golang code completion in VS Code without installing Go?

My goal is to use Go exclusively on Docker. In other words, I try my best not to install Go directly to my computer. And, I am writing the code on a VS Code and use the "Go" extension.
The problem is that when I create a main.go it throws an error:
Failed to find the "go" binary in either GOROOT() or PATH(/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin). Check PATH, or Install Go and reload the window. If PATH isn't what you expected, see https://github.com/golang/vscode-go/issues/971
Is there an alternative to get the code completion feature in VS Code?
This should be achievable using the Remote Development Extension pack. VSCode has good documentation on using containers as your development enviroment. This will allow you to enjoy all of the Go extension's features without needing Go to be installed directly on your machine. You can opt to install the extensions in the container as well.
See Developing inside a Container

which ide now can support go1.18 workspace

I use this command to init workspace
go work init mypkg example
My code can run but the goland 2021.3 can not support it very well.Is there a way to solve it?
I believe goland has its own parsing implementation, so you will have to wait for an official update.
If you want to try out go1.18beta you can use VSCode or any other editor/IDE which uses gopls. The go extension in VSCode uses gopls which provides some support for generics and workspaces. It is not perfect yet, but that is to be expected with beta software.

GoLand package index breaks when using multiple versions of the same module

I need to use both a v1 and alpha version of the google.golang.org/api/compute api in a project I'm working on. The need to use the alpha version came up after already using the v1 version for quite a bit of code, and I need to use both versions at once.
When I import the alpha version, GoLand indexing breaks and I lose auto completion and get a bunch of red text/lines in my packages that import alpha. Though, go will still build/run the code just fine.
I've tried clearing my GoLand cache and re-indexing, but am still having the same issue. I've also cleaned my go cache with go clean -modcache and go mod tidy to download and checksum everything again.
Is there something I'm missing that would make this an issue in GoLand, but my go code still builds and runs fine?
You can navigate to the package sources by pressing Command/CTRL+Click on the import statement and find compute-gen.go file and size limit warning. The IDE behaves as expected.
As a workaround, you can invoke Help | Edit Custome Properties... and add the following line idea.max.intellisense.filesize=8500000, restart GoLand. But please keep in mind that the IDE can be slow when dealing with large files even if they're not open in the editor.
You can read more about the idea.properties file here.

Shortcut to install libraries in Goland

I'm following the instructions in https://golang.org/doc/code.html#Library and trying to use Goland. I'm surprised though that I can't find a fast way to install the library I'm writing. According to the tutorial, you should use install to install your code in the pkg or bin folders, yet I can't find a way to do this in Goland other than writing it in the console.
What am I missing?
There are two ways to import Go packages using GoLand:
copy-paste the code that needs the package into the IDE and then on the import declaration use Show Intention to see the list of actions available and choose the first one, go get -t <package-name>/...
copy the URL of the Github repository into your clipboard, switch to the IDE and use the pop-up to run go get on the package. The pop-up disappears after a few seconds.
I've attached the image which shows these options.
This will download libraries in the correct GOPATH directory and run go install as part of the go get action.
As for installing the libraries that you are developing in GOPATH/pkg, there is no need for that, as soon as you run any configuration from the IDE, be it an application or a test that depend on those libraries, the IDE will install those libraries as well.

How can I import a library from github to GO playground?

Hi I want to import a 3rd party library to GO playground, I saw an answer for this question: https://stackoverflow.com/a/27813778/6638204 but it said that this can not be done, but the xiam/go-playground library on github states that it can do this. I tried using it but I did not understand what should I exactly do. I successfully installed the library and used it to run programs that do not need third party libraries. but I did not get how can I import a third party library. ps: I have docker installed in my machine
The answer is still the same: you can't do that on the "official" Go Playground (at https://play.golang.org/).
If you or someone else runs a custom, modified version of the Go Playground: then the answer is you can do there whatever is allowed, which may include the usage of external libraries which the custom engine may go get prior to compilation and execution.
Also see related question: Which packages may be imported in the go playground?
You might be able to do it since May 14th, 2019
See this tweet (from Brad Fitzpatrick)!
The #golang playground now supports third-party imports, pulling them in via https://proxy.golang.org/
Example: https://play.golang.org/p/eqEo7mqdS9l 🎉
Multi-file support & few other things up next.
Report bugs at golang/go issue 31944, or here on the tweeters.
But that means you have published your package deliverable in such a way it is reference by the Go proxy.

Resources