Using -mod flag in go CLI - go

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.

Related

GoConvey: command not found : goconvey [duplicate]

I want to install packages from github to my $GOPATH, I have tried this:
go get github.com:capotej/groupcache-db-experiment.git
the repository is here.
Command go
Download and install packages and dependencies
Usage:
go get [-d] [-f] [-t] [-u] [-v] [-fix] [-insecure] [build flags] [packages]
Get downloads the packages named by the import paths, along with their
dependencies. It then installs the named packages, like 'go install'.
The -d flag instructs get to stop after downloading the packages; that
is, it instructs get not to install the packages.
The -f flag, valid only when -u is set, forces get -u not to verify
that each package has been checked out from the source control
repository implied by its import path. This can be useful if the
source is a local fork of the original.
The -fix flag instructs get to run the fix tool on the downloaded
packages before resolving dependencies or building the code.
The -insecure flag permits fetching from repositories and resolving
custom domains using insecure schemes such as HTTP. Use with caution.
The -t flag instructs get to also download the packages required to
build the tests for the specified packages.
The -u flag instructs get to use the network to update the named
packages and their dependencies. By default, get uses the network to
check out missing packages but does not use it to look for updates to
existing packages.
The -v flag enables verbose progress and debug output.
Get also accepts build flags to control the installation. See 'go help
build'.
When checking out a new package, get creates the target directory
GOPATH/src/. If the GOPATH contains multiple entries, get
uses the first one. For more details see: 'go help gopath'.
When checking out or updating a package, get looks for a branch or tag
that matches the locally installed version of Go. The most important
rule is that if the local installation is running version "go1", get
searches for a branch or tag named "go1". If no such version exists it
retrieves the default branch of the package.
When go get checks out or updates a Git repository, it also updates
any git submodules referenced by the repository.
Get never checks out or updates code stored in vendor directories.
For more about specifying packages, see 'go help packages'.
For more about how 'go get' finds source code to download, see 'go
help importpath'.
This text describes the behavior of get when using GOPATH to manage
source code and dependencies. If instead the go command is running in
module-aware mode, the details of get's flags and effects change, as
does 'go help get'. See 'go help modules' and 'go help module-get'.
See also: go build, go install, go clean.
For example, showing verbose output,
$ go get -v github.com/capotej/groupcache-db-experiment/...
github.com/capotej/groupcache-db-experiment (download)
github.com/golang/groupcache (download)
github.com/golang/protobuf (download)
github.com/capotej/groupcache-db-experiment/api
github.com/capotej/groupcache-db-experiment/client
github.com/capotej/groupcache-db-experiment/slowdb
github.com/golang/groupcache/consistenthash
github.com/golang/protobuf/proto
github.com/golang/groupcache/lru
github.com/capotej/groupcache-db-experiment/dbserver
github.com/capotej/groupcache-db-experiment/cli
github.com/golang/groupcache/singleflight
github.com/golang/groupcache/groupcachepb
github.com/golang/groupcache
github.com/capotej/groupcache-db-experiment/frontend
$
First, we need GOPATH
The $GOPATH is a folder (or set of folders) specified by its environment variable. We must notice that this is not the $GOROOT directory where Go is installed.
export GOPATH=$HOME/gocode
export PATH=$PATH:$GOPATH/bin
We used ~/gocode path in our computer to store the source of our application and its dependencies. The GOPATH directory will also store the binaries of their packages.
Then check Go env
You system must have $GOPATH and $GOROOT, below is my Env:
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/elpsstu/gocode"
GORACE=""
GOROOT="/home/pravin/go"
GOTOOLDIR="/home/pravin/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"
Now, you run download go package:
go get [-d] [-f] [-fix] [-t] [-u] [build flags] [packages]
Get downloads and installs the packages named by the import paths, along with their dependencies. For more details you can look here.
Note that since Go 1.17 installing packages with go get is deprecated:
Building and installing packages with get is deprecated. In a future release, the -d flag will be enabled by default, and go get will be only be used to adjust dependencies of the current module. To install a package using dependencies from the current module, use go install.
This "future release" is Go 1.18, as mentioned in the release notes:
go get no longer builds or installs packages in module-aware mode. go get is now dedicated to adjusting dependencies in go.mod. Effectively, the -d flag is always enabled.
(The -d flag instructs go get to only download packages and not install them.)
Use go install instead:
# Install the latest version of a program,
# ignoring go.mod in the current directory (if any).
$ go install golang.org/x/tools/gopls#latest
# Install a specific version of a program.
$ go install golang.org/x/tools/gopls#v0.6.4
# Install a program at the version selected by the module in the current directory.
$ go install golang.org/x/tools/gopls
# Install all programs in a directory.
$ go install ./cmd/...
The Go 1.18 release notes also mention that go get will work as before with GO111MODULE=off. However in 2022 you should definitely migrate to modules and use go install instead.
Run it from console
go mod download

Issue in installing a go package

So,I recently started following a video tutorial and i am fairly new to golang and tried installing the forked version of bolt db using
$ go get go.etcd.io/bbolt/...
Note : I want to use this specific version
but i am getting an error which says
go: go.mod file not found in current directory or any parent directory.
'go get' is no longer supported outside a module.
To build and install a command, use 'go install' with a version,
like 'go install example.com/cmd#latest'
For more information, see https://golang.org/doc/go-get-install-deprecation
or run 'go help get' or 'go help install'
I read a few GitHub issues which say that go get is deprecated so how do I resolve this ?
I also tried few other things such as
go install go.etcd.io/bbolt/...
Go modules are today's standard. Especially if you are new to Go; do not spend time on material that do not use (and teach) them.
Run go mod init yourproject
in your project repository root directory. This will create go.mod file.
Once you have that you can either:
import go.etcd.io/bbolt in source code and then run go mod tidy. Go tool will find and add module to your dependencies (go.mod file). This is described in Getting started tutorial.
run go get go.etcd.io/bbolt directly, that will update dependencies too.
Using Go Modules series explains workflow in detail and will be helpful when converting commands from an outdated material.

go run doesnt get the latest tag

I'm playing with GraphQL in Go and I was trying to get gqlgen tool using the familiar go run command.
I was expecting it to retrieve the latest available tag, instead I"m somehow getting a tag which does not seem to exist in the repo at all:
$ go run github.com/99designs/gqlgen version
v0.9.1-dev
I would expect the above to return per the latest tag
v0.13.0
Go version installed on my workstation:
$ go version
go version go1.15.5 darwin/amd64
Anyone has any ideas what's going on?
UPDATE: disabling GOPROXY does not help
UPDATE2: it turns out the version is hardcoded into version.go as you can see here, but even if go run gets the master instead of the latest tag, you'd still expect the output to be 0.13.0-dev as per the master branch Instead I suspect there is some string ordering of versions of tags which orders 0.9.* above 0.1*.*
go run does not currently support running a specific version of a binary, but note that there is an accepted Go proposal to add that functionality in a future release of the go command.
As of Go 1.16, you can instead go install the binary at a specific version, and then run that binary from its install location (either $(go env GOBIN) or $(go env GOPATH)/bin).
$ GOBIN=~/bin go install github.com/99designs/gqlgen#latest
$ ~/bin/gqlgen ...
I think the version command was misconfigured on build, and that v0.9.1-dev was not the tag at build time.

How can I install a specific version of golint for use globally?

I'm trying to create a docker image for use in a build pipeline that has various tools pre-installed for building and testing go projects. One tool we need is golint but I'm struggling to see how to install a specific version of it. The reason I want to lock down the version is to avoid accidental / unwanted / unintended breakages at a later date.
For a start, looking here the versions are not exactly in an easy-to-type format.
Also when I try to use the following command
go get -u golang.org/x/lint/golint#v0.0.0-20181217174547-8f45f776aaf1
I get an error
go: cannot use path#version syntax in GOPATH mode
Googling has so far yielded very few relevant results...
Is what I'm trying to do possible? Many thanks!
You need to be in go module mode to get code of a specific version, since in addition to downloading the code, the version is recorded in the go module file.
The easiest way to do this would be to create an empty directory, run go mod init, which will create a go.mod file.
Then, you can run go get golang.org/x/lint/golint#v0.0.0-20181217174547-8f45f776aaf1, which will add golint at that version to your go.mod file. You can then run go install golang.org/x/lint/golint from within that directory, which will install golint at the version specified into your $GOBIN directory (which defaults to $GOPATH/bin).

Limit VS Code for Go to download packages only for v 1.11

I am using Visual Studio Code version 1.33.1 as IDE for our Go applications. We would like to use Go version 1.11 for our applications. However looks like one or more of the dependencies we are using has downloaded a package for Go 1.12. Now, VS Code is failing to build the application with the following error:
go build golang.org/x/sys/unix: module requires Go 1.12 go build
github.com/pelletier/go-toml: module requires Go 1.12
go [1,1]
I tried re-installing Go 1.11, removed the offending packages and let it reinstall. No matter when I tried to build VS Code is downloading the 1.12 version fails to build.
I would like VS Code not to download 1.12 version of the packages and restrict it to 1.11 only.
Go 1.12 Release Notes (February 2019)
Modules
The go directive in a go.mod file now indicates the version of the
language used by the files within that module. It will be set to the
current release (go 1.12) if no existing version is present. If the go
directive for a module specifies a version newer than the toolchain in
use, the go command will attempt to build the packages regardless, and
will note the mismatch only if that build fails.
This changed use of the go directive means that if you use Go 1.12 to
build a module, thus recording go 1.12 in the go.mod file, you will
get an error when attempting to build the same module with Go 1.11
through Go 1.11.3. Go 1.11.4 or later will work fine, as will releases
older than Go 1.11. If you must use Go 1.11 through 1.11.3, you can
avoid the problem by setting the language version to 1.11, using the
Go 1.12 go tool, via go mod edit -go=1.11.
$ go help go.mod
The go.mod file itself is line-oriented, with // comments but
no /* */ comments. Each line holds a single directive, made up of a
verb followed by arguments. For example:
module my/thing
go 1.12
require other/thing v1.0.2
require new/thing/v2 v2.3.4
exclude old/thing v1.2.3
replace bad/thing v1.4.5 => good/thing v1.4.5
The verbs are
module, to define the module path;
go, to set the expected language version;
require, to require a particular module at a given version or later;
exclude, to exclude a particular module version from use; and
replace, to replace a module version with a different module version.
A possible solution to your problem was first introduced in Go1.12: the go.mod verb go, to set the expected language version.
UPDATE:
Comment: I tried using the suggested command: go mod edit -go=1.11 I
got an error: flag provided but not defined: -go I manually edited to
add go 1.11 right under my module declaration in all go.mod files, it
did not work. –
user2995358
Your results are expected. As I explained before and the documentation states, the go.mod verb go was first introduced in Go1.12.
For example, the expected results,
$ go version
go version go1.11.10 linux/amd64
$ go mod edit -go=1.11
flag provided but not defined: -go
usage: go mod edit [editing flags] [go.mod]
Run 'go help mod edit' for details.
$
$ go version
go version go1.12.5 linux/amd64
$ go mod edit -go=1.11
$
Read the documentation:
$ go version
go version go1.11.10 linux/amd64
$ go1.11 help modules
Preliminary module support
Go 1.11 includes preliminary support for Go modules,
including a new module-aware 'go get' command.
We intend to keep revising this support, while preserving compatibility,
until it can be declared official (no longer preliminary),
and then at a later point we may remove support for work
in GOPATH and the old 'go get' command.
$
$ go version
go version devel +004fb5cb8d Fri May 3 03:49:11 2019 +0000 linux/amd64
$ go help modules
Module support
Go 1.13 includes official support for Go modules,
including a module-aware 'go get' command.
Module-aware mode is active by default.
$
Go 1.11 includes only preliminary support for Go modules. Go 1.13 includes full official support for Go modules.
Why do you expect everything to work flawlessly in Go1.11 with only preliminary support?

Resources