How to install "gotests" command? - go

I need to use the test driven development in Go using "gotests" command.
gotests -all *
This is not working. I did go get -u /github.com/cweill/gotests
and go install. But there is no binary created in $GOPATH/bin.

since there is NO main package, Use this command
$ go get github.com/cweill/gotests/...
this itself download all dependencies for the current package, and creates bin file, after downloading this package. see in $GOPATH/bin there will be a bin file named gotests
for more see HERE

The following worked for me with go v1.19.1
go install github.com/cweill/gotests/gotests#latest
Using go get to install things has been disabled since 1.18. See Deprecation of 'go get' for installing executables

go install github.com/rakyll/gotest
Source: https://github.com/rakyll/gotest

Related

Why does `go install` on a Github directory work but not file?

So if I have a go module on my local filesystem, I can install a binary by running go install cmd/main.go and it will create a binary called $GOPATH/main as expected. Basically it behave the same as go build but copying the binary to $GOPATH.
However, when I try to install the same module on stored on Github, it fails:
go install github.com/sanbornm/go-selfupdate/cmd/go-selfupdate/main.go
no required module provides package github.com/sanbornm/go-selfupdate/cmd/go-selfupdate/main.go; to add it:
go get github.com/sanbornm/go-selfupdate/cmd/go-selfupdate/main.go
Instead, I need to call go install on the directory for it to properly work:
go install github.com/sanbornm/go-selfupdate/cmd/go-selfupdate
How do I reconcile this behavior difference?

Can 'go install' be made to work for executables with different names from the git repo?

Go has a nice feature where you can go install <x> and it will download, build and install a binary.
For example, on my local windows PC, go install github.com/goreleaser/goreleaser will find the latest release for goreleaser, download, build and install it to my local binaries path.
I am working on a project where we would like to enable go install, but encounter a problem if the github repo name does not match the executable name. The GitHub CLI itself runs into the exact same problem:
Example:
go install github.com/cli/cli#latest
go: downloading github.com/cli/cli v1.14.0
go: github.com/cli/cli#latest: module github.com/cli/cli#latest found (v1.14.0), but does not contain package github.com/cli/cli
Is there a way to resolve this?
Update: I worked out that I could directly reference the package via it's sub directory. In my particular instance this works: go install github.com/OctopusDeploy/cli/cmd/octopus#latest
This is a bit unpleasant, but works correctly. It doesn't work for the github CLI because their go.mod has a replace directive in it :-(
Question: Can this be made nicer? Is there a way to put some sort of alias or configuration file so that go install github.com/OctopusDeploy/cli#latest can be used instead of go install github.com/OctopusDeploy/cli/cmd/octopus#latest ?
Can this be made nicer? Is there a way to put some sort of alias or configuration file so that go install github.com/OctopusDeploy/cli#latest can be used instead of go install github.com/OctopusDeploy/cli/cmd/octopus#latest ?
No. Dead simple.

go install #latest found but does not contain package

I'm trying to install my package using go install but I get this error message when running the command go install github.com/JoaoDanielRufino/gcloc/cmd/gcloc#latest:
go install: github.com/JoaoDanielRufino/gcloc/cmd/gcloc#latest: module github.com/JoaoDanielRufino/gcloc#latest found (v1.0.0), but does not contain package github.com/JoaoDanielRufino/gcloc/cmd/gcloc
I want the executable name to be gcloc.
Here is the current source code: https://github.com/JoaoDanielRufino/gcloc
Note: I've already tried go clean -modcache but it didn't work
As the main function of this package isn't on its root, you should pass the directory of the main package on your command.
So, your command will be:
go install -v github.com/JoaoDanielRufino/gcloc/cmd#latest
I came across a similar issue when I was trying to use go install to install the cloudflare/cf-terraforming tool on my machine. The documentation for this tool is not clear on the installation and I had to dig around to get this to work
Basically #Jictyvoo answer above sums it up, if the path is pointing to anything other than directory where the main.go file is sitting I got the error
Command: go install github.com/cloudflare/cf-terraforming#latest v0.8.0#latest
go: github.com/cloudflare/cf-terraforming#latest: module
github.com/cloudflare/cf-terraforming#latest found (v0.8.0), but does not
contain package github.com/cloudflare/cf-terraforming
when I switched to the below it worked fine for me:
Command: go install -v github.com/cloudflare/cf-terraforming/cmd/cf-terraforming#latest
This worked for me after checking the repo and realising that the main.go file was sitting in the cmd/cf-terraforming subdirectory

Difficulty installing go buffalo using go mod on windows

I am very new to golang. I am trying to work with the gomod. Trying to explore the go buffalo framework. But finding a bit of difficulty in installing that.
What I have done:
I saw that go get is nomore supported for buffalo and so switched to go modules.
Created a module by go mod init github.com/webbuffalotest
Fetched go get -v github.com/gobuffalo/buffalo (on the same directory where I have go.mod file)
Fetched go get -v github.com/mattn/go-sqlite3 (on the same directory where I have go.mod file)
go install github.com/gobuffalo/buffalo
I was expecting a buffalo.exe inside %GOPATH%/bin so that I can add it to my path but didn't find one.
My question is what's wrong? Is the exe not installed or it's somewhere else because of go mod. Any help will be highly appreciated.
I am using windows 10. I am not willing to install package managers as scoop or choco to install buffalo. Thanks for your patience :)
Edited:
Also tried setting set GO111MODULE=on but of no use.
Solved:
My bad, I should have used go install github.com/gobuffalo/buffalo/buffalo instead of go install github.com/gobuffalo/buffalo
github.com/gobuffalo/buffalo is a library; the corresponding binary is (aptly-named) github.com/gobuffalo/buffalo/buffalo.
The go install command you ran should have warned you about that, but didn't because go install used to also be used to cache compiled libraries (it no longer does that in module mode).
I've filed https://golang.org/issue/46912 to add a diagnostic.

Golang equivalent of npm install -g

If I had a compiled Golang program that I wanted to install such that I could run it with a bash command from anywhere on my computer, how would I do that? For example, in nodejs
npm install -g express
Installs express such that I can run the command
express myapp
and express will generate a file directory for a node application called "myapp" in whatever my current directory is. Is there an equivalent command for go? I believe now with the "go install" command you have to be in the directory that contains the executable in order to run it
Thanks in advance!
Update: If you're using Go 1.16, this answer still works, but go install has changed and is now the recommended method for installing executable packages. See Karim's answer for an explanation: https://stackoverflow.com/a/68559728/10490740
Using Go >= 1.11, if your current directory is within a module-based project, or you've set GO111MODULE=on in your environment, go get will not install packages "globally". It will add them to your project's go.mod file instead.
As of Go 1.11.1, setting GO111MODULE=off works to circumvent this behavior:
GO111MODULE=off go get github.com/usr/repo
Basically, by disabling the module feature for this single command, it will install to GOPATH as expected.
Projects not using modules can still go get normally to install binaries to $GOPATH/bin.
There's a lengthy conversation and multiple issues logged about this change in behavior branching from here: golang/go - cmd/go: go get should not add a dependency to go.mod #27643.
Starting with Go >= 1.16 the recommended way to install an executable is to use
go install package#version
For example, go install github.com/fatih/gomodifytags#latest.
Executables (main packages) are installed to the directory named by the GOBIN environment variable, which defaults to $GOPATH/bin or $HOME/go/bin if the GOPATH environment variable is not set. You need to add this directory to your PATH variable to run executables globally. In my case, I've added this line to my ~/.zshrc file. (if you are using bash, add it to the ~/.bash_profile file):
export PATH="$HOME/go/bin:$PATH"
Go team published a blog post about this change, here's the explanation quote:
We used to recommend go get -u program to install an executable, but this use caused too much confusion with the meaning of go get for adding or changing module version requirements in go.mod.
Refer to go install documentation for more details
As far as I know, there is no direct equivalent to npm install -g. The closest equivalent would not be go install, but go get. From the help page (go help get):
usage: go get [-d] [-f] [-fix] [-insecure] [-t] [-u] [build flags] [packages]
Get downloads and installs the packages named by the import paths,
along with their dependencies.
By default, go get installs binaries to $GOPATH/bin, so the easiest way to make those binaries callable from everywhere is to add that directory to your $PATH.
For this, put the following line into your .bashrc (or .zshrc, depending on which shell you're using):
export PATH="$PATH:$GOPATH/bin"
Alternatively, you could also copy or link the executables to /usr/local/bin:
ln -s $GOPATH/bin/some-binary /usr/local/bin/some-binary
Short solution for Linux users:
Use the go get command as usual
Add the following lines to .bashrc:
# This is the default GOPATH, you should confirm with the 'go env' command
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
Restart terminal or source it. Installed binaries will be available globally.
For Go v1.8+
go install package_name#latest
Caveat: this answer is outdated following the 2020 deprecation of go get. The solution presented here won't work with newer Go runtime installs.
The closest analogue of this in Go would be go get. By default, it will fetch a Go package from a supplied repository URL, and requires a $GOPATH variable to be set in your shell so that Go knows where to store the packages (and subsequently where to find them when compiling code depending on go get-ted packages).
Example syntax:
$ go get github.com/user/repo
The behaviour supplied by npm's -g flag is default, and packages installed using go get are normally available globally.
See go get --help for more information about the command.
As mentioned by #helmbert, adding your $GOPATH to your $PATH is useful if you're installing standalone packages.
if you are using zsh :
first: install your package using :
go install package#version
then , you edit your .zshrc file
nano ~/.zshrc
Add this line to the end of .zshrc file :
export PATH="$HOME/go/bin:$PATH"
last but not least :
source ~/.zshrc
then open a new terminal and execute your command :)
TL;DR at the bottom. I'm going to walk you through how I came to this conclusion, and why the more obvious solutions don't work.
Upon seeing this question, I thought "If I could set root's GOPATH=/usr, it would install things in /usr/bin/ and /usr/src!"
So I tried the obvious thing:
Add GOPATH=/usr to root's .bashrc.
And it worked!
Sort of.
Not really.
Turns out, sudo doesn't execute root's .bashrc. For "security" or something like that.
Do env_set or something in /etc/sudoers
Turns out, /etc/sudoers can only remove environment variables. There's no env_set directive.
(As far as I can find)
Dig through man sudoers.
Where does sudo get it's default set of environment variables from?
Well, the first one in the list is /etc/environment, so that's the one I used.
sudo echo "GOPATH=/usr" >> /etc/environment
sudo go get <repo>
Binaries will be put in /usr/bin, and sources will be put in /usr/src.
Running go as non-root will use GOPATH the "normal" way.
If you don't have go installed, you may use the gobinaries. it builds an on-demand binary of the project from github repo.
The command to install the go package would be:
curl -sf https://gobinaries.com/rakyll/hey | sh

Resources