Auto download new package in golang module project [duplicate] - go

This question already has answers here:
cannot find package "rsc.io/quote"
(4 answers)
Closed 1 year ago.
I am following a golang book (using go 1.15) which not surprisingly advocates using go modules for dependency management. I have no problem following along until a chapter where it says "because we have module enabled for our project, after adding a new import to our code, we can just do go run and go is clever enough to notice the new import and will auto download the package as well as update the go.mod file for us. We don't need to manually do go get". In book, it appears that this auto download does work as there is screenshot in the book showing the download message.
To clarify, this new import is added for the first time to the project.
This is totally new to me and got me excited but when I tried it, it doesn't work. It says no required module provides package github.com/xxx/yyy; to add it: go get github.com/xxx/yyy;
I obviously have module enabled for the project.
Is there any setting I missed to enable this auto download?
I am using go 1.16

When you create a new project, you must manually add the libraries that you will use via the command:
go get ...
But later, if you run that project in another machine, for example, once the go run command or the go build command, Go will download and add the libraries automatically.
For example, if you download a project from GitHub and run it, it will load and add all the libraries automatically.
All you have to do is type: go run ... or go build

Related

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

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

How do you know if your github golang repo is being used?

I have an API client written in go. It's on github at the moment with about 4 stars, but I have no idea whether there are other developers using this package.
There's nothing listed if you go to the repository's Insights->Dependency Graph. I don't think that this is enabled for go projects.
What's the best way to find out whether a golang package is being imported by another golang package/application?
You can't really tell what private projects use your repo.
For public modules, you may use go.dev. Go to pkg.go.dev, enter your package import path, then click on the "Imported By" tab.

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 do I use github packages in go?

Sorry, very much a newbie golang question. I have a github project named journalbeat that I am modifying.
When I clone that repository I can set the GOPATH and run go get to grab all the imports and it places them into src.
I have a new import I want to add.
import "github.com/danwakefield/fnmatch"
but it doesn't grab it. How does simply running go get determine whether something is downloaded or not?
And finally, the vendoring system is used. How do I populate that with the fnmatch? do I create it manually? it all seems very cumbersome.
I thought go get was meant to make all this easy?
Try instead a dependency manager: the most recent and actively developed one is golang/dep.
Reading dep "issue" 943, use:
dep ensure
That will set up vendored dependencies through import analysis, and you can configure locking those down if need be.
Try go get with the verbose flag -v like:
go get -v github.com/danwakefield/fnmatch
This will show you more details. Post the result here.
we use Glide package management tool for GO. Go check it out gitHub link

Go Wants to Import Package From Comment [duplicate]

New Go programmer here -- apologies if this is well worn territory, but my google searching hasn't turned up the answer I'm looking for.
Short Version: Can I, as a programmer external to the core Go project, force my packages to be imported with a specific name. If so, how?
Long Version: I recently tried to install the bcrypt package from the following GitHub repository, with the following go get
go get github.com/golang/crypto
The package downloaded correctly into my workspace, but when I tried to import it, I got the following error
$ go run main.go main.go:10:2: code in directory /path/to/go/src/github.com/golang/crypto/bcrypt expects import "golang.org/x/crypto/bcrypt"
i.e. something told Go this package was supposed to be imported with golang.org/x/crypto/bcrypt. This tipped me off that what I actually wanted was
go get golang.org/x/crypto/bcrypt
I'd like to do something similar in my own packages — is this functionality built into Go packaging? Or are the authors of crypto/bcrypt doing something at runtime to detect and reject invalid package import names?
Yes it's built in, I can't seem to find the implementation document (it's a relatively new feature in 1.5 or 1.6) however the syntax is:
package name // import "your-custom-path"
Example: https://github.com/golang/crypto/blob/master/bcrypt/bcrypt.go#L7
// edit
The design document for this feature is https://docs.google.com/document/d/1jVFkZTcYbNLaTxXD9OcGfn7vYv5hWtPx9--lTx1gPMs/edit
// edit
#JimB pointed out to https://golang.org/cmd/go/#hdr-Import_path_checking, and in the go1.4 release notes: https://golang.org/doc/go1.4#canonicalimports

Resources