Unable to automatically install go extensions in vscode - go

I am getting started with Go and would like to use Visual Studio Code for editing. I successfully installed go on my Ubuntu Computer.
My GOPATH (go env) is set to the same value as go.gopath in settings.json. If I run Go: Current GOPATH it outputs the correct path.
However, I am not able to install the go extensions. If I click on Install all I get output similar to this:
Installing 1 tool at home/jan/go/bin
dlv
Installing github.com/go-delve/delve/cmd/dlv FAILED
1 tools failed to install.
dlv:
Error: Command failed: /snap/bin/go get -u -v github.com/go-delve/delve/cmd/dlv
I seem to be able to just install packages manually with /snap/bin/go get -u -v github.com/go-delve/delve/cmd/dlv. The command runs without errors, but VSCode seems to be unable to find dlv.
My settings.json:
{
"terminal.integrated.rendererType": "dom",
"go.useLanguageServer": true,
"go.formatTool": "gofmt",
"go.lintOnSave": "file",
"go.vetOnSave": "package",
"go.buildOnSave": "package",
"go.lintTool": "golint",
"go.gopath": "home/jan/go"
}
I have git installed on my computer. It works without a problem.

Your GOPATH is not set correctly in settings.json.
"go.gopath": "home/jan/go"
This path is relative, and so it would be appended to whatever your working directory is, with the obvious result of not being able to find the path as it won't exist.
You can either set it correctly, or delete it and the GOPATH environment variable will be used. (I run it this way.)
"go.gopath": "/home/jan/go"

Related

Not able to watch go files after installing Compile Daemon

Installed using, go get github.com/githubnemo/CompileDaemon and go install github.com/githubnemo/CompileDaemon
When I try to run it using -> CompileDaemon --compile="./folderName"
It gives -> zsh: command not found: CompileDaemon
Note:
I'm using oh-my-zsh.
My GOPATH and GOBIN are not default one's, I set it to goWorkspace
Install Compile Daemon using
"go install github.com/githubnemo/CompileDaemon#latest"
Important:
Ensure that the Compile Daemon executable path(i.e: GOBIN) is included in $PATH.
You can include it by editing your ".bashrc" or ".zshrc" file.
or by "export PATH=$PATH:$GOBIN"
First, don't forget that the use of go get to build and install packages is deprecated (In earlier versions of Go, 'go get' was used to build and install packages. Now, 'go get' is dedicated to adjusting dependencies in go.mod.)
All you need should be go install:
go install github.com/githubnemo/CompileDaemon#latest
That should generate a CompileDaemon executable in your directory named by the GOBIN environment variable.
But you need $GOBIN itself in your $PATH (from your ~/.zshrc for instance):
export PATH="$PATH:$GOBIN"

VS Code Go: $GOPATH/go.mod exists but should not

I'm trying to use the Go features of VS Code (like 'Go to definition') in a package with a go.mod. However, if I go into the repository's main directory (with the go.mod) and code ., I get this error:
Error loading workspace folders (expected 1, got 0) failed to load view for file:///Users/kurt/Documents/http-enrollment-portal: err: exit status 1: stderr: $GOPATH/go.mod exists but should not
I've read that I might have to update the gopls language server, so I pressed Cmd + Shift + P and selected "Go: Install/Update Tools" and selected gopls; however, this gives me the same error:
go.toolsGopath setting is not set. Using GOPATH /Users/kurt/Documents/http-enrollment-portal:/Users/kurt/go
Installing 1 tool at /Users/kurt/Documents/http-enrollment-portal/bin in module mode.
gopls
Installing golang.org/x/tools/gopls FAILED
1 tools failed to install.
gopls:
Error: Command failed: /usr/local/opt/go#1.12/bin/go get -v golang.org/x/tools/gopls
$GOPATH/go.mod exists but should not
$GOPATH/go.mod exists but should not
The strange thing is that 'Go to definition' does work in other repositories. I suppose a possible solution is to set the go.toolsGopath?
go env -w GOPATH=
GOPATH and Modules
this link
When using modules, GOPATH is no longer used for resolving imports. However, it is still used to store downloaded source code (in GOPATH/pkg/mod) and compiled commands (in GOPATH/bin).
I had a similar issue. So to explain I am using https://www.mongodb.com/blog/post/quick-start-golang-mongodb-starting-and-setup
To try Mongodb with Go. In VS Code I first installed go mod init quickstart
Then I used go get go.mongodb.org/mongo-driver and got the error $GOPATH/go.mod exists but should not.
I had previously set my GOPATH according to https://www.freecodecamp.org/news/setting-up-go-programming-language-on-windows-f02c8c14e2f/ (see Phase 3 on this page).
I went into my Windows Environment Variables and deleted my GOPATH. On the Windows 10 start menu go to System then Advanced system settings under the Advanced tab click the Environment Variables button and I deleted my GOPATH variable.
Next back in VS Code I again try go get go.mongodb.org/mongo-driver and it works.
you have to go to the folder where you created your go project and delete the go.mod file after you create another file with the command : go mod init nameproject on your command prompt (power

vscode does not detect errors golang

My vscode does not detect errors in golang.
Example:
package somepackage
import "fmt"
func f(name string) string {
name = 1
return name
}
This should throw a type error, but it does not. I never get any errors.
My settings.json contains
"go.gopath": "some/path",
"go.vetOnSave": "package",
"go.lintOnSave": "package",
"go.testOnSave": true,
"go.buildOnSave": "package",
"go.coverOnSave": true
I was able to run go: install/update Tools. All tools are installed successfully.
I am also able to run debug a .go file in vscode.
As #pwaterz pointed out, the solution to my problem was to add "go.goroot: /some/other/path".
The reason that vscode was not able to detect errors was, that there are different go versions on my computer. Adding the goroot and running go: install/update Tools solved the problem.
---- Edit: Multiple go versions ----
Had multiple conflicting go versions on my Mac, pulled in via brew. Fixed the problem with a reinstall.
Uninstall go and also run brew uninstall go
Reinstall go
Set environment variables in your .bash_profile or similar. Compare here.
Apply the changes to your profile by running e.g. source .bash_profile
Restart VSCode
In settings.json set "go.goroot": "/usr/local/go"
Run go: Toggle workspace trust space to make sure changes to settings.json are applied (you have to trust your workspace for that)
go: Install/update tools and select all
---- Edit: Incorrect root folder ----
Make sure that you open the root folder of your project and not a sub-folder of your project. That may cause in invalid import paths otherwise
---- Edit: Broken language server ----
Try to run go: restart language server
You may see Error loading workspace
run go mod tidy and try again

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

How to properly install Cabal 1.18.02 in Windows 7?

I'm trying to install the new Cabal in Windows 7. It successfully installs 1.18.02, but when I run "cabal --version" it shows 1.16. The folder C:\Users\me\AppData\Roaming\cabal\bin is at the front of my PATH.
When I directly run the 1.18 executable, I get a bunch of "cabal.exe does not exist" errors.
How can I configure Cabal to use version 1.18 by default?
There is a path conflict due the way haskell platform installs cabal.exe
As seen in this ticket
To fix it:
Delete `C:/program files(x86)/Haskell platform/lib/extralibs/bin/cabal.exe.`
In any case, you can use search tool to find which cabal.exe to delete.
I used cabal-dev to install cabal 1.18 to a sandbox, such that C:\cabal-dev\cabal-dev\bin contains the latest (1.18) cabal executable. I then set C:\Cabal-dev\cabal-dev\bin as the first Variable value in the "Path" variable in "systems variables" (as opposed to the "PATH" variable in "user variables"). That worked for me, and allowed me to keep the older build of cabal intact, (in case its needed later).
It worked for me. Good luck. Here are my exact steps:
Install cabal-dev
open command prompt anywhere
execute following command: “cabal install cabal-dev”
Install new cabal
create directory: c:\cabal-dev, cd to it
execute following command: “cabal-dev install cabal-install”
Set new, upgraded cabal path as higher priority
Close any open command prompts
run: Sysdm.cpl
Go to the “Advanced” tab
Click “Environment Variables”
Select the “Path” variable listed in the “System variables” (lower) windows
Click edit
Add following string to the beginning of the variable value:
“C:\Cabal-dev\cabal-dev\bin;”
Ok, Ok, Ok
Ensure latest cabal is being run:
Open command prompt (anywhere except c:\cabal-dev\ *)
execute following command: “cabal –V”
cabal version should be 1.18.0.2
One other possible cause of this error (aside from the cabal in extralibs problem described in another answer) is that the ld in your path can't read the .o files that GHC produces.
I hit this problem with GHC 7.8.1 RC1, and could see this going wrong by running cabal install with -v3. The 'ld' on my path was coming from Haskell Platform 2013.2.0.0.
Adding mingw\bin from the new GHC's directory to the path fixed the problem.

Resources