In Go, unlike in C# where System.Diagnostics.Debugger.IsAttached can be used to detect debugger presence, there is no direct equivalent. To address this, one approach is to leverage build tags with the delve debugger.
Create two files:
// +build delve package isdelve const Enabled = true
// +build !delve package isdelve const Enabled = false
In your main program, import the isdelve package and check the Enabled constant:
import "isdelve" func main() { fmt.Println("delve", isdelve.Enabled) }
In GoLand, navigate to 'Run/Debug Configurations', and in 'Go tool arguments', add:
-tags=delve
Now, running the program in GoLand will enable the delve build tag, allowing you to access the isdelve.Enabled constant.
Alternatively, use delve's set command to set a variable after starting the debugger:
dlv debug a.go (dlv) set debug.enabled true
The above is the detailed content of How Can I Detect if the GoLand Debugger is Attached to a Go Program?. For more information, please follow other related articles on the PHP Chinese website!