How does Go update third-party packages?

Go

Go Problem Overview


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?

Go Solutions


Solution 1 - Go

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.

Solution 2 - Go

@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

Solution 3 - Go

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/...

Solution 4 - Go

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

Solution 5 - Go

To specify versions, or commits:

go get -u otherpackage@1.2.3

go get -u otherpackage@git-sha

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

Solution 6 - Go

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

Solution 7 - Go

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

Solution 8 - Go

Go to path and type

go get -u ./...

It will update all require packages.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionshalakhinView Question on Stackoverflow
Solution 1 - Gotux21bView Answer on Stackoverflow
Solution 2 - GoSalvador DaliView Answer on Stackoverflow
Solution 3 - GodtxView Answer on Stackoverflow
Solution 4 - GoAusView Answer on Stackoverflow
Solution 5 - GoLuke WView Answer on Stackoverflow
Solution 6 - GoF566View Answer on Stackoverflow
Solution 7 - GoToidView Answer on Stackoverflow
Solution 8 - Gokiki_ygnView Answer on Stackoverflow