Reputation: 9649
To my knowledge go distribution
comes with some sort of package manager
. After go 1.4.1
installation I've run go help
in order to find any sub-command capable of listing locally installed go packages
, but unfortunately got none.
So how to do it?
Upvotes: 66
Views: 129770
Reputation: 1
If you have a specific project to work on, you can just run "cat go.mod" under that project directory.
e.g
head go.mod
module github.com/cilium/cilium
go 1.17
require (
github.com/Azure/azure-sdk-for-go v59.3.0+incompatible
github.com/Azure/go-autorest/autorest v0.11.22
github.com/Azure/go-autorest/autorest/adal v0.9.17
github.com/Azure/go-autorest/autorest/azure/auth v0.5.9
github.com/Azure/go-autorest/autorest/to v0.4.0
Upvotes: -1
Reputation: 350
go list ...
is quite useful, but there were two possible issues with it for me:
go.mod
files), and I don't care about other packages lying around (which may have been installed just to try them out). go list ...
doesn't help with that.So here's a somewhat different take. Assuming all projects are under ~/work
:
find ~/work -type f -name go.mod \
-exec sed $'/^require ($/,/^)$/!d; /^require ($/d;/^)$/d; /\\/\\/ indirect$/d; s/^\t+//g' {} \; \
| cut -d' ' -f1 \
| sort | uniq
A line by line explanation:
go.mod
filessed
to each file to filter its content as follows (explained expression by expression):
require(
... )
chunksrequire(
and )
lines, so just lines with packages remain1) Note the sed expression argument uses bash quoting to escape the TAB character as "\t" for readability over a literal TAB.
Upvotes: 2
Reputation: 1209
Start Go documentation server:
godoc --http :6060
Visit http://localhost:6060/pkg
There will be list of all your packages.
When you install new ones they do not appear automatically. You need to restart godoc
.
Upvotes: 32
Reputation: 417402
goinstall
is now historygoinstall
was replaced by go get
. go get
is used to manage external / 3rd party libraries (e.g. to download them, update them, install them etc).
Type go help get
to see command line help, or check out these pages:
About the go command (blog post)
If you want to list installed packages, you can do that with the go list
command:
To list packages in your workspace, go to your workspace folder and run this command:
go list ./...
./
tells to start from the current folder, ...
tells to go down recursively. Of course this works in any other folders not just in your go workspace (but usually that is what you're interested in).
Executing
go list ...
in any folder lists all the packages, including packages of the standard library first followed by external libraries in your go workspace.
If you also want to see the imported packages by each package, you can try this custom format:
go list -f "{{.ImportPath}} {{.Imports}}" ./...
-f
specifies an alternate format for the list, using the syntax of package template
. The struct whose fields can be referenced can be printed by the go help list
command.
If you want to see all the dependencies recursively (dependencies of imported packages recursively), you can use this custom format:
go list -f "{{.ImportPath}} {{.Deps}}" ./...
But usually this is a long list and just the single imports ("{{.Imports}}"
) of each package is what you want.
Also see related question: What's the Go (mod) equivalent of npm-outdated?
Upvotes: 108
Reputation: 1027
on *nix systems (possibly on windows with bash tools like msysgit or cmder), to see what packages I have installed, I can run:
history | grep "go get"
But thats messy output. For whatever reason I decided to see of i could clean that output up a little so i made an alias for this command:
history | grep 'go get' | grep -v ' history ' | sed -e $'s/go get /\\\\\ngo get /g' | grep 'go get ' | sed -e $'s/-u //g' | sed -e $'s/-v //g' | sed -e $'s/ &&//g' | grep -v '\\\n' | egrep 'get [a-z]' | sed -e $'s/go get //g' | sed -e $'s/ //g' | sort -u
please don't ask why I did this. Challenge maybe? Let me explain the parts
history
the history
grep "go get"
grep over history and only show lines where we went and got something
grep -v " history "
and remove times when we have searched for "got get" in history
sed -e $'s/go get /\\\\\ngo get /g'
Now we take any instances of "go get " and shove a new line in front of it. Now they're all at the beginning.
grep "go get "
filter only lines that now start with "go get"
sed -e $'s/-u //g'
and sed -e $'s/-v //g'
remove flags we have searched for. You could possibly leave them in but may get duplicates when output is done.
sed -e $'s/ &&//g'
some times we install with multiple commands using '&&' so lets remove them from the ends of the line.
grep -v "\\\n"
my output had other lines with newlines printed I didnt need. So this got rid of them
egrep "get [a-z]"
make sure to get properly formatted go package urls only.
sed -e $'s/go get //g'
remove the "go get " text
sed -e $'s/ //g'
strip any whitespace (needed to filter out duplicates)
sort -u
now sort the remaining lines and remove duplicates.
This is totally untested on other systems. Again, I am quite sure there is a cleaner way to do this. Just thought it would be fun to try.
It would also probably be more fun to make a go ls
command to show the actual packages you explicitly installed. But thats a lot more work. Especially since i'm only still learning Go.
Output:
> gols
code.google.com/p/go.crypto/bcrypt
github.com/golang/lint/golint
github.com/kishorevaishnav/revelgen
github.com/lukehoban/go-find-references
github.com/lukehoban/go-outline
github.com/newhook/go-symbols
github.com/nsf/gocode
github.com/revel/cmd/revel
github.com/revel/revel
github.com/rogpeppe/godef
github.com/tpng/gopkgs
golang.org/x/tools/cmd/goimports
golang.org/x/tools/cmd/gorename
gopkg.in/gorp.v1
sourcegraph.com/sqs/goreturns
Upvotes: 0