Matthew Mallimo
Matthew Mallimo

Reputation: 371

GoLang debug console application

I am trying to debug this project

I am using visual studio code, and have the Go extension setup. I am able to set a breakpoint in the main function and debug it, but I never see the visual command prompt. I used Delve, ran the exe that the project produces, and attached. This allowed me to debug it, but I would prefer to debug it in vscode.

I tried using this vscode debug configuration:

    {
        "name": "Launch file",
        "type": "go",
        "request": "attach",
        "mode": "exec",
        "program": "${workspaceFolder}/lazygit.exe"
    },

and it is successful. But again, I cannot see the actual command window and actually use the project.

Is there any way to attach to an already open process in vscode, like in delve, or for vscode to launch the exe in a command window?

Upvotes: 0

Views: 3321

Answers (3)

Krzysztofz01
Krzysztofz01

Reputation: 597

I had a similar problem trying to debug an application that generated some kind of UI in the terminal. I managed to get the effect that after starting the debugger from VSCode, the program compiled and ran in a separate console, allowing me to test UI rendering and IO operations. My lanuch.json looks like this:

"version": "0.2.0",
    "configurations": [
        {
            // Other not related properties...
    
            "request": "launch",
            "mode": "debug",
            "console": "externalTerminal" // The important one
        }
    ]

Upvotes: 0

Moshe Be
Moshe Be

Reputation: 1

Make sure you disable the compiler optimization in your go build command. It should be something like:

go build -gcflags="all=-N -l"

Upvotes: 0

Himanshu
Himanshu

Reputation: 12675

You are using the .exe file for debugging the code. Use the raw code to denug the application. Also there is a debug console where you can see the output when debugging using breakpoints or in case of any error. The configurations for launch.json should be:

{
    "name": "Launch",
    "type": "go",
    "request": "launch",
    "mode": "debug",
    "remotePath": "",
    "port": 2345,
    "host": "127.0.0.1",
    "program": "${workspaceFolder}",
    "env": {},
    "args": [],
    "showLog": true
}

Debug console will show the output of debugging and stdout:

enter image description here

Upvotes: 1

Related Questions