简体   繁体   中英

Using -mod flag in go CLI

I was reading through godoc on how I can keep my dependencies up to date: https://golang.org/ref/mod#build-commands

It says that -mod=mod flag can be used to automatically update the go.mod file. But I am not able to use it.

This is the command that I tried:

% go get -mod=mod ./..                                 
flag provided but not defined: -mod
usage: go get [-d] [-t] [-u] [-v] [-insecure] [build flags] [packages]
Run 'go help get' for details.

I am obviously missing something because I can't seem to get the flag to work.

Edit: It's a documentation error

After some experiments, it looks that only quite old versions of Go understand go get -mod= , in particular version 1.11. So the documentation is outdated and you could report it.

Officially recommended on Go version 1.14 or newer: to automatically update an existing go.mod file and to download dependencies, instead of doing go get -mod=mod. , simply run:

go get -d .

For the sake of the example being complete, you could now actually build everything and put binaries into $GOBIN (or $GOPATH/bin ) with:

go install

If it still doesn't work, a couple of things to check:

Update go to the latest version

The online documentation that you are reading is always about the latest official version, while you might be using an older version. Check your version:

go version

With the current pace of Go development, most people are trying to update as soon as they can. Follow https://golang.org/doc/install

Docs for the older version

Apparently, there is no easy way to read older documentation online. Instead, I use godoc tool to do it locally:

go get -v  golang.org/x/tools/cmd/godoc
godoc -http=127.0.0.1:6060

Leave the above command running , then in your browser go to http://127.0.0.1:6060/cmd/go/

This way I've checked for example what the old docs said about -mod flag.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM