How do I control where `go get` puts things? - go

When I go get a package (say, godoc), it wants to create and install it in /usr/lib/go/bin (this is an Ubuntu system using the stock Go packages).
This isn't cool, because like a good Debian user I only let .deb packages install stuff into /usr.
How do I convince it to install into either my GOPATH workspace, or somewhere under /usr/local?

The correct answer turned out to be installing the golang-go.tools package.

If you install Go with GVM instead of from apt they will be placed in ~/.gvm/gos/ or your $GOPATH instead.
https://github.com/moovweb/gvm
Also I don't think godoc comes with 1.3 anymore, it has to be installed from go.tools
http://godoc.org/code.google.com/p/go.tools/cmd/godoc
http://golang.org/doc/go1.2#go_doc
The "go doc" command is deleted.

Related

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.

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.

Can't install any GoLang script in Linux

When I try to run any Go script it show me this error
I installed go lang step by step from this link
https://www.tecmint.com/install-go-in-linux/
When I setup go script like this
go get github.com/tomnomnom/waybackurls
I got error like this
github.com/tomnomnom/waybackurls
src/github.com/tomnomnom/waybackurls/main.go:191: u.Hostname undefined
(type *url.URL has no field or method Hostname)
If you are following the guide you linked by copy-pasting commands, you will have installed Go 1.7.3. The function url.Hostname() was added in Go 1.8.
I suggest completely ignoring that guide. Remove /usr/local/go, remove ~/go_projects and undo the path related stuff.
Instead, use the package manager of your OS to install Go.
Most likely, this means you should do either sudo apt install golang (for Ubuntu, Debian, ...) or sudo dnf install golang (Fedora, CentOS, ...).
That will give you the latest version that is supported by distro maintainer (which at the moment is probably 1.11 or 1.12, depending on your distro).
As an alternative to the packagemanager, download the latest version from https://golang.org/dl/.
This approach also gives you an installation that follows the Go ecosystem their conventions for paths (I'm not sure if ~/go_projects was ever a think, but it isn't today).

What's the proper way to install a go package to the system path?

I'm trying to install a go project (the keybase client, in particular), but I want to install it globally for all the users on my system.
I've searched around and found a few people who accidentally install things to /usr/local/go/bin or something to that effect, but no instructions on how I actually should be installing things to such locations.
But that's what I want to do - globally install the application. How should I be doing that with go?
I'm not using any of the pre-built packages because none exist for Raspbian Jesse.
Have you tried setting $GOBIN like:
GOBIN=/usr/local/bin/ or GOBIN=/usr/bin/ or any directory in the PATH?
and then:
export GOBIN
and then install the app?
According to the manual the default $GOBIN is $GOROOT/bin so I guess those users set GOROOT to /usr/local/go
The approach that I've taken is this:
PATH="$PATH:/usr/local/go/bin" GOPATH=/usr/local/go/ go get github.com/keybase/client/go/keybase
PATH="$PATH:/usr/local/go/bin" GOPATH=/usr/local/go/ go install -tags production github.com/keybase/client/go/keybase
Which ends out installing keybase to /usr/local/go/bin/keybase. It's possible that there's a better way, but so far I haven't seen one.

How to install "gotests" command?

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

Resources