Register nvim-dap-go
mainTo register the Go adapter and debug configurations, call the setup() function in your Neovim configuration (init.vim or init.lua).
lua require('dap-go').setup()repository·main·Indexed 20 days ago
https://github.com/leoluz/nvim-dap-goAn extension for nvim-dap that provides specialized configurations for debugging Go applications using Delve (dlv). It simplifies debugging individual tests via Treesitter and provides utilities to pass arguments and build flags during the launch process.
To register the Go adapter and debug configurations, call the setup() function in your Neovim configuration (init.vim or init.lua).
lua require('dap-go').setup()Instead of defining all configurations in Neovim, you can use a .vscode/launch.json file in your project root. nvim-dap will automatically load this file if it exists. This is useful for setting dynamic ports or complex remote attachment settings.
Example .vscode/launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Remote debug API server",
"type": "go",
"request": "attach",
"mode": "remote",
"port": 4444,
"host": "127.0.0.1",
"substitutePath": [
{
"from": "${workspaceFolder}",
"to": "/usr/src/app"
}
]
}
]
}Use the following commands to debug tests based on your cursor position using Treesitter:
require('dap-go').debug_test(): Runs the closest test located above the cursor.require('dap-go').debug_last_test(): Runs the test that was most recently executed.It is recommended to map these to keys for easier access.
" Recommended mapping
nmap <silent> <leader>td :lua require('dap-go').debug_test()<CR>Install nvim-dap-go using your preferred Neovim plugin manager.
nvim-dap installeddelve (dlv) >= 1.7.0 in your PATH:TSInstall go if using nvim-treesitter)-- Using vim-plug
Plug 'leoluz/nvim-dap-go'
-- Using packer.nvim
use 'leoluz/nvim-dap-go'You can customize the plugin by passing a configuration table to setup().
dap_configurationsA list of tables representing additional nvim-dap configurations. Each entry must have type = "go" to be recognized.
delveSettings for the Delve debugger:
path: Path to the dlv executable (defaults to "dlv" on PATH).initialize_timeout_sec: Seconds to wait for Delve to initialize (default: 20).port: Port for the debugger (default: "${port}" for a random available port).args: Additional arguments passed to dlv.build_flags: Build flags passed to Delve (e.g., "-tags=unit"). Note: passing build flags via args is ineffective in dap mode.detached: Whether the dlv process is created detached. Defaults to true on non-Windows systems. On Windows, Delve < 1.24.0 may require this to be false.cwd: Current working directory for running dlv.testsverbose: Boolean to enable verbosity when running tests (default: false).require('dap-go').setup {
dap_configurations = {
{
type = "go",
name = "Attach remote",
mode = "remote",
request = "attach",
},
},
delve = {
path = "dlv",
initialize_timeout_sec = 20,
port = "${port}",
args = {},
build_flags = {},
detached = vim.fn.has("win32") == 0,
cwd = nil,
},
tests = {
verbose = false,
},
}To create custom debug configurations that prompt the user for input during the launch process, attach the following functions to the args or buildFlags fields in your dap_configurations:
require('dap-go').get_arguments: Prompts for command-line arguments.require('dap-go').get_build_flags: Prompts for build flags.require("dap-go").setup({
dap_configurations = {
{
type = "go",
name = "Debug (Build Flags & Arguments)",
request = "launch",
program = "${file}",
args = require("dap-go").get_arguments,
buildFlags = require("dap-go").get_build_flags,
},
}
})