Skip to content

Instantly share code, notes, and snippets.

@andremartinsds
Last active January 7, 2025 02:17
Show Gist options
  • Save andremartinsds/fe514464bbca34ce514e7fc9ab146a4e to your computer and use it in GitHub Desktop.
Save andremartinsds/fe514464bbca34ce514e7fc9ab146a4e to your computer and use it in GitHub Desktop.
[golang] - debugging golang with delve

How to Debug a Golang Application with Delve on Visual Studio Code

To debug your Golang application using Delve in Visual Studio Code, follow these steps:

  1. Install Delve:

    go install github.com/go-delve/delve/cmd/dlv@latest
  2. Ensure Go Path is Set in Your Shell: Add the following lines to your shell configuration file (e.g., .zshrc or .bashrc):

    . ~/.asdf/plugins/golang/set-env.zsh
    
    export GOPATH=$HOME/.asdf/installs/golang/1.22.10
    export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

    Then, reload your shell configuration:

    source ~/.zshrc  # Use ~/.bashrc for Bash
  3. Add Delve Path to Visual Studio Code Settings: Open Visual Studio Code settings and update your settings.json file with the following configuration:

    {
      "go.alternateTools": {
        "dlv": "/home/martins/.asdf/installs/golang/1.22.10/bin/dlv"
      }
    }
  4. Create Debug Configuration: Create or update the .vscode/launch.json file in your project directory with the following configuration:

    {
      "version": "0.2.0",
      "configurations": [
        {
          "name": "golang debug",
          "type": "go",
          "request": "launch",
          "mode": "auto",
          "program": "${workspaceFolder}/cmd/server", // Path to application entry point
          "env": {
            "APP_PORT": "#",
            "DB_HOST": "#",
            "DB_USER": "#",
            "DB_PASSWORD": "#",
            "DB_NAME": "#",
            "DB_PORT": "#",
            "JWT_SECRET": "#",
            "JWT_EXPIRES_IN": "#"
          },
          "args": [],
          "cwd": "${workspaceFolder}"
        }
      ]
    }

With these steps completed, you can press F5 in Visual Studio Code to start debugging your application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment