Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot Install Delve Go Debugger on Mac

I am trying to follow along with the following YouTube video on getting started with Go Debugging.

It recommends following the Delve installation instructions on the official Delve github repo. For Mac users, they are as follows:

Making sure the toolchain is in place

xcode-select --install
xcode-select: error: command line tools are already installed, use "Software Update" to install updates

Using "go get" to install Delve

go get -u github.com/go-delve/delve/cmd/dlv

Making sure developer mode is enabled in Xcode

sudo /usr/sbin/DevToolsSecurity -enable
Developer mode is already enabled.

To check that the installation has been completed correctly, I tried running the following in my Go project directlry:

dlv debug
zsh: command not found: dlv

The author of the video tutorial recommends updating GOPATH and PATH variables in the ~/.bash_profile file in the case that the command is not recognized. I did so so by adding:

export GOPATH=/Users/<user_name>/go/src/
export PATH=$PATH:/Users/<user_name/go/src/my_project

However, even after doing so, I get the same result when trying to run the debugger:

dlv debug
zsh: command not found: dlv

Even if I change the shell for the default zsh to bash, using exec bash, I get the same result.

like image 809
HMLDude Avatar asked Nov 29 '19 20:11

HMLDude


People also ask

How do I install Golang debugger?

Step 1: Go delve can be easily downloaded and installed just by using the go get command inside a workspace but if you are using go modules then you might have to execute this command (in the image below) outside the project directory so as to avoid Delve being added inside your go mod file which has been executed now.

How do I use Golang debugger?

To debug a program, execute the dlv debug command. Attach the filename at the end of this command, and debugging will start. For example, if you want to debug the main.go file, run the command dlv debug main.go . It is also known as “delve server” because this is a running process waiting for instructions.

What is delve Golang?

About Delve Delve is a debugger for the Go programming language. The goal of the project is to provide a simple, full featured debugging tool for Go. Delve should be easy to invoke and easy to use. Chances are if you're using a debugger, things aren't going your way.

Why use delve for go debugging?

Being tailored specifically for Go, Delve has intricate knowledge of the Go runtime and provides features and an environment not available in other debuggers. The tool aims for simplicity of use, staying out of your way as you figure out what's going wrong with your program.

How do I install the Go language debugger delve on RHEL?

Starting in the RHEL 8.2 and devtools-2020.2 releases, the Go language debugger Delve will be installed with the Go toolchain itself via the go-toolset package. Being tailored specifically for Go, Delve has intricate knowledge of the Go runtime and provides features and an environment not available in other debuggers.

Is it possible to debug Golang code on Mac?

Debugging plugin on mac is really needed for Golang developers. Hope delve supports it :) Sorry, something went wrong. I didn't try implementing this because it would be affected by golang/go#25841 at least.

How to install go delve in Linux?

Step 1: Go delv e can be easily downloaded and installed just by using the go get command inside a workspace but if you are using go modules then you might have to execute this command (in the image below) outside the project directory so as to avoid Delve being added inside your go mod file which has been executed now.


Video Answer


2 Answers

In order to run an executable, it needs to be available in your PATH.

1. Configure your path.

Make sure that your GOPATH and $GOPATH/bin directories are set correctly in your shell environment. You can do this by adding the following lines to your shell configuration.

~/.zshrc if you're using zsh.

~/.bash_profile if you're using bash.

export GOPATH="$HOME/go"
export PATH="$GOPATH/bin:$PATH"

2. Re-load your shell configuration.

Make sure to either restart your shell or run source on your shell configuration file after the changes:

source ~/.zshrc if you're using zsh.

source ~/.bash_profile if you're using bash.


3. Install the dlv package.

go install github.com/go-delve/delve/cmd/dlv

This is assuming that you're using /Users/<username>/go as your GOPATH.


You should now be able to run dlv from your terminal session.

Good luck!

like image 186
Nick Corin Avatar answered Oct 29 '22 16:10

Nick Corin


Set the environment variable GOBIN to be where you want the dlv binary to be installed.

For example:

GOBIN=~/bin go install github.com/go-delve/delve/cmd/dlv

This will install dlv in ~/bin

Clarification

When you run go install, the installation path can be specified by setting the GOBIN environment variable.

There are two ways to set the environment variable:

1) Run export GOBIN=<SOMETHING> before you run go install ..

$ export GOBIN="$HOME/bin"
$ go install github.com/go-delve/delve/cmd/dlv

The export command will alter the environment in the current terminal session. Any later command you execute will see the value you set for GOBIN

When you go with this approach, you usually want to have this environment variable active not only in this session, but all future sessions as well. So it's better to add the line to your bash profile.

2) Set the environment variable only for the command.

$ A=10 some-command

In this case, some-command will see the value of the environment variable A set to '10'. If you run a later command, it will not see this value.

This approach is useful when you are just trying things out, or if you only want to set certain environment variables in certain situations.

The command line I provided as the answer follows this second approach.

It sets the GOBIN variable to the ~/bin directory, and then invokes go install in the same line. This way, this invocation of go install will install dlv in ~/bin

This of course assumes you have a bin directory in your home directory.

If you don't have such directory, then this will not work.

The idea is not to copy paste the line as is. The idea is to change ~/bin to be the directory where you would like the dlv binary to be installed.

like image 39
hasen Avatar answered Oct 29 '22 16:10

hasen