261

Looking how actively golang packages grow and improve I wonder how the problem with package versions is solved?

I see that one way is to store third-party packages under a project folder.

But what if I install it with go get?

9 Answers 9

400

go get will install the package in the first directory listed at GOPATH (an environment variable which might contain a colon separated list of directories). You can use go get -u to update existing packages.

You can also use go get -u all to update all packages in your GOPATH

For larger projects, it might be reasonable to create different GOPATHs for each project, so that updating a library in project A wont cause issues in project B.

Type go help gopath to find out more about the GOPATH environment variable.

5
  • 2
    is it possible to update all packages to latest version, kind of like bundle update?
    – chourobin
    May 15, 2013 at 2:41
  • 81
    should just be go get -u all sorry to resurrect, but just in case anyone else was looking. Apr 18, 2014 at 20:11
  • 1
    go get -u all no longer works! Running this command in the GOPATH, at least for Go 1.13, will return a warning: `warning "all" matched no packages"
    – user3827326
    Sep 8, 2019 at 23:50
  • Works with Go 1.12.9
    – fviktor
    Sep 11, 2019 at 14:27
  • 3
    Go 1.16.3, go get -u all doesn't work. Is there a replacement?
    – dimitarvp
    Apr 19, 2021 at 21:13
100

@tux answer is great, just wanted to add that you can use go get to update a specific package:

go get -u full_package_name
1
  • 1
    For updating a specific branch of the package: go get -u full_package_name.BRANCH_NAME
    – Misagh
    Dec 23, 2020 at 7:45
25

Since the question mentioned third-party libraries and not all packages then you probably want to fall back to using wildcards.

A use case being: I just want to update all my packages that are obtained from the Github VCS, then you would just say:

go get -u github.com/... // ('...' being the wildcard). 

This would go ahead and only update your github packages in the current $GOPATH

Same applies for within a VCS too, say you want to only upgrade all the packages from ogranizaiton A's repo's since as they have released a hotfix you depend on:

go get -u github.com/orgA/...
14

The above answeres have the following problems:

  1. They update everything including your app (in case you have uncommitted changes).
  2. They updated packages you may have already removed from your project but are already on your disk.

To avoid these, do the following:

  1. Delete the 3rd party folders that you want to update.
  2. go to your app folder and run go get -d
11

To specify versions, or commits:

go get -u [email protected]

go get -u otherpackage@git-sha

See https://github.com/golang/go/wiki/Modules#daily-workflow

6

Since this is one of the top hits when googling, I just wanted to add that for 1.17 "installing executables with 'go get' in module mode is deprecated".

  • go get -d
    • For dependencies of the current module
  • go install
    • For requirements of the current module
  • go install <with_version>
    • To install ignoring the current module, i.e: 'go install example.com/cmd@latest'

https://golang.org/doc/go-get-install-deprecation

4

go 1.13

(exec from module root directory)

Update specified dependencies:

go get -u <package-name>

Update all direct and indirect dependencies to latest minor or patch upgrades (pre-releases are ignored):

go get -u ./...
# or
go get -u=patch ./...

Reference:

https://github.com/golang/go/wiki/Modules#daily-workflow

go help get

1
  • What is the actual difference between go get -u and go get -u ./... In theory it should be the same but in practice go get -u ./... recursively updates all the dependencies of dependencies (major versions) in my project and can cause a bunch of errors.
    – JCMS
    Jan 25, 2021 at 19:59
3

If you want to upgrade a version from a specific branch, you can use:

go get -u <path-to-repo>@<branch>
2

Go to path and type

go get -u ./...

It will update all require packages.

2
  • This literal interpretation does not work. Some more context would be helpful.
    – user3827326
    Sep 8, 2019 at 23:56
  • 6
    I do believe he meant go get -u ./... (3 dots) Dec 31, 2019 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.