This question already has answers here:
Go update all modules
(3 answers)
Closed 1 year ago.
the go mod tidy adds package dependencies to the go.mod file. How we can update them automatically without manually editing go.mod file, e.g. by removing some entries? For instance, if I use make I want to add a similar command that can update all dependencies of my package/repo and then compile the code with latest version of package dependencies.
In order to update all the dependencies you need to use:
go get -u
And then go mod tidy.
go get -u updates all packages and their dependencies, changing the go.mod.
Related
I am new to go and have trouble using a library that does not have a go.mod file. The library is https://github.com/yourbasic/graph and I tried installing it according to the instructions given in the go docs. Since I cannot make a request for the version on the repo, I used the #latest flag, so in order to install, I executed
go install github.com/yourbasic/graph#latest
This however fails and says
package github.com/yourbasic/graph is not a main package
Is there a proper way to install libraries like that? I assume my way of proceeding to just copy the files into a directory within my project is not very clean.
As #JimB said: instead of attempting to install it,
Import the package in your code where applicable.
Run go get github.com/yourbasic/graph#latest — which will download the module (to the local cache) and update your module's go.mod file.
Build.
The steps 1 and 2 can be swapped, but then the generated go.mod entry will have the // indirect comment which will disappear next time you run go mod tidy.
This question already has answers here:
what is the purpose of `go mod vendor` command?
(3 answers)
Closed 8 months ago.
I have a go monorepo with a few packages that at one point had an import path like github.com/user/ticketing/tickets/models/connection, but is a private repo and I'd rather not have to depend on the remote repo anyway for deployment on kubernetes/skaffold. I was wondering how when running go mod tidy, I can prevent tidy from trying to download the local packages but instead have everything point to the local packages instead through masking/shadowing. I'm open to a bazel-based solution as well, but I feel like tidy would not be impacted by anything around bazel. Thank you.
If you do not want to depend on the availability of a remote repository the usual practice it to do vendoring of the dependencies. Similar questions have already been asked: what is the purpose of `go mod vendor` command?
This question already has answers here:
How can I automatically add dependency packages to go.mod
(2 answers)
Closed 9 months ago.
Suppose I have main.go in directory project/ and have subdirectory project/pkg/mydb/ that is used by main.go.
To add a dependency to my code inside mydb/ I should run go get ... in that subdir mydb/ or in top-level project directory (project/)?
Also where main.go should reside: directly in project/ or in project/src/?
Go only think in term of module and packages. Usually your module would be project/ and can be composed of one or several packages (project/pkg/mydb can be one of them)
Only go modules have dependencies. So you should run go get in project
main.go can be wherever you want, it will just change whether you need to run go build . or go build ./src
(This is only applicable if you use go modules, so if you have a go.mod in your project. But if you should be using them anyway)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
When I install a package using go get, in the golang module mode, these are installed in the go root folder.
I would like to be able to install the modules in the same way that npm or yarn does for node.js, global or local.
what would be the right way to achieve this behavior.
Thanks.
As others have mentioned, you can create your module files like this:
go mod init [import path]
After that you can run this command any time to move all dependencies into the vendor directory:
go mod vendor
This is very similar to the behaviour of the node_modules directory.
To build using the dependencies in this directory, you can add -mod vendor to your build command:
go build -mod vendor
This way you can copy your project to any machine without go-getting anything on that other machine.
According to the docs, the -mod=vendor flag is set automatically if the Go version in the go.mod file is 1.14 or higher and a vendor directory exists.
To clean unused dependencies from the vendor directory, you can run
go mod tidy
There is little bit different approach. Go lock versions based on github (gitlab, etc) current commit. When you want to use specific versions of packages and not master all the time you want to use go modules (this is official way to do it, but there are some other tools). You init module in you project
go mod init [possible custom package name]
And everytime you call "go get somepackage" it writes versions it uses to a file called go.mod.
You can read more here
I've installed a package using go modules (go get in Go 1.13) and now I want to remove it. In the documentation there is nothing about this and in go get docu neither.
Removing the package from go.mod manually doesn't solve the issue so it remains in go.sum.
How should I remove a package in a clean way?
Found it https://go.dev/blog/using-go-modules#removing-unused-dependencies
go mod tidy
So basically, once the package is not being imported in any package you can perform a go mod tidy and it will safely remove the unused dependencies.
And if you are vendoring the dependencies, then run the command below to make the module changes be applied in the vendor folder:
go mod vendor
#jesugmz answer doesn't say, what if you wanna remove a currently using package in go modules.
So, if you're using go modules (you have a go.mod file in your project) and you want to remove a currently using package, check $GOPATH/pkg/mod/ directory and simply remove the package named package#version.
For example, if you have github.com/some/project package installed, you should run the following command:
rm -rf $(go env GOPATH)/pkg/mod/github.com/some/project#v1.0.0
You can find the using package version in go.mod file.