Issue in installing a go package - go

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.

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.

Understanding go mod and cause of package is not in GOROOT

I'm trying to play around with the lightning network. I have cloned the repo, and on disk placed it here (I'm using windows):
C:\Users\hallibut\Documents\GitHub\lnd
I'd like to run any of the tests in itest, lets say testMultiHopPayments. The cli commands I'm using after I cd into the above location is:
go test itest -run testMultiHopPayments
However, I keep getting the error:
package itest is not in GOROOT (C:\Program Files\Go\src\itest)
I've read through the various posts on this error, but I'm still not quite sure why it happens, and it's likely because I don't fully understand the go module (I'm new to go). This article, was probably the best in helping me understand the structure and env variables:
https://golangbyexample.com/workspace-hello-world-golang/
My understanding from the various readings is that whatever directory the go.mod file is in, indicates the module level directory. Prior to version 1.13 there was a required directory and structure, but now that should not be an issue if you're using at least version 1.13 and modules. I'm using 1.17.1. This is somewhat of an assumption or inference, but I believe everything lower in the directory structure is part of a package to be installed as part of the module (and is defined by the package keyword). However, I don't understand why a package with source code within a subdirectory would be missing/throw the aforementioned error. I've also tried running:
go mod install github.com/lightningnetwork/lnd/lntest/itest
That doesn't seem to do anything/has not effect on the error. What am I not understanding about packages? Looking at the go.mod file I observe that itest is not specifically defined anywhere… Not sure if that's required. Also, I assume I've got to run some build process prior? I attempted this with:
go install -v ./...
If you're using VS Code and Go Modules, you need to "Open folder" and point to the cloned repo, to get around that error

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.

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).

`go install` adds record in `go.mod`

I'm a bit confused about how Go modules work on installing binaries using go install.
I tried to install (https://github.com/joho/godotenv) binary by executing go install github.com/joho/godotenv/cmd/godotenv and I found out that it adds a record in the go.mod.
I'm lost as we don't use this package in the code and after running go mod tidy it gets deleted (as it is not it the code).
Can someone explain is it expected behaviour of go modules?
Secondly, how I could avoid adding it to the go.mod as we only need to install and execute the binary?
Thanks.
Go version: go version go1.13.4 darwin/amd64
Command go: The go.mod file:
The go command automatically updates go.mod each time it uses the module graph, to make sure go.mod always accurately reflects reality and is properly formatted.
The go tool will update go.mod automatically when it detects dependencies are inaccurate when performing a build.
When you install github.com/joho/godotenv/cmd/godotenv from your module, this install requires at least the package in question being built / installed (and also its dependencies, transitively).
You may safely run go mod tidy to undo the recording of this "one-time" dependency.
In general if you want to disallow the go tool to update the go.mod file, you may use the -mod=readonly flag, but that would fail go install ("can't load package: package xxx: import lookup disabled by -mod=readonly"). You can read more about this here: Go Wiki: Go modules: Can I control when go.mod gets updated and when the go tools use the network to satisfy dependencies?
Alternatively, if you want to avoid this, build / install your tools outside of your module. You may use a "dummy" module for this.

Resources