10

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.

jeremysprofile
  • 10,028
  • 4
  • 33
  • 53
HMLDude
  • 1,547
  • 7
  • 27
  • 47

2 Answers2

28

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!

Nick Corin
  • 2,214
  • 5
  • 25
  • 46
  • what is the purpose of the semi-colon at the end of the second line? – HMLDude Nov 29 '19 at 21:36
  • Followed the steps you suggested, both with and without the semi-colon. Still getting the same result ```zsh: command not found: dlv``` – HMLDude Nov 29 '19 at 21:38
  • Sorry, the `;`is a typo. You can leave it out. I will edit the post. – Nick Corin Nov 30 '19 at 07:16
  • After you run the `go install`, is the executable inside `$GOPATH/bin`? – Nick Corin Nov 30 '19 at 07:16
  • If you have added `$GOPATH/bin` to your `PATH`, and the executable is in `$GOPATH/bin` - I'm not sure why you're getting troubles. Are you sure you are editing the correct shell config? i.e. `.zshrc` if you're using `ZSH` and `.bash_profile` if you're using `bash`. – Nick Corin Dec 01 '19 at 06:36
  • I've only edited ```.bash_profile```. Then I tried running it from both ```bash``` and ```zsh```, switching between the two with the ```exec``` command. – HMLDude Dec 02 '19 at 22:57
  • If `zsh` is your main shell, then rather add the lines into your `~/.zshrc` file. If you're wanting to use bash, then use `chsh` to change your default shell to `bash` and restart your terminal session. – Nick Corin Dec 03 '19 at 05:20
  • I haven't used `dlv` previously, as I usually debug using Goland. I just followed by own instructions and successfully debugged some tests that I'm having issues with using `dlv` - I'm using `zsh` and `Mac OS`. – Nick Corin Dec 03 '19 at 07:01
  • This looks perfect, should work fine. (I have the same setup, MacOS and Zsh) – Sufiyan Parkar Dec 05 '19 at 07:49
  • Following your updated instructions, I now get a new error message: ``` can'tload package: package .: no Go files in /Users//go/bin exit status 1``` – HMLDude Dec 09 '19 at 23:52
  • Did you replace `` with your username? – Nick Corin Dec 10 '19 at 06:37
  • Yes, I replaced user_name – HMLDude Dec 10 '19 at 16:19
  • Can you post the output of `echo $GOPATH` and `echo $PATH`? – Nick Corin Dec 11 '19 at 05:59
10

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.

hasen
  • 161,647
  • 65
  • 194
  • 231
  • Should I write this as ```export GOBIN=~/...``` or just insert it as you have listed it in my ```source ~/.bash_profile```? – HMLDude Dec 09 '19 at 23:55
  • You can do it inline like I showed here without the export keyword. If you want it to always be set then you can add it to your bash profile as `export GOBIN=~/bin` – hasen Dec 10 '19 at 05:06
  • After adding your line, with the ```export``` and then running ```source ~/.bash_profile```, I get the following error message: ```bash: export: `github.com/go-delve/delve/cmd/dlv': not a valid identifier ``` – HMLDude Dec 10 '19 at 16:21
  • Define "basic understanding"? – HMLDude Dec 10 '19 at 23:04
  • In your bash profile, only put `export GOBIN=~/bin`. Don't put the `go install ..` as part of the `export` line. – hasen Dec 10 '19 at 23:49
  • I added some clarifications. Does it clear things up, or do you still have some confusions? – hasen Dec 11 '19 at 00:00
  • Looks like we are making some progress now. I am running ```dlv debug```` from my ```~./go/bin``` file, but I am getting the following errror: ```can't load package: package .: no Go files in /Users//go/bin exit status 1```. Despite the fact that I have several compiled go binaries in that folder. – HMLDude Dec 13 '19 at 20:16