skip to Main Content

I am trying to debug a .go file in VSCode with Go v1.20.3.
In the left panel of vscode, clicked on Debug icon > click on the Gear Icon to create a configuration file.(launch.json)

My launch.json file shown below.(created automatically)

"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
// "program": "${fileDirname}"
"program": "${workspaceRoot}"

I changed the original "program": "${fileDirname}" to "program": "${workspaceRoot}" hoping one of them would work.

when I do F5 (running debugger), in the Debug Console (Terminal section), I get the following error:

Starting: C:UsersShawngobindlv.exe dap --listen=127.0.0.1:60824 from C:UsersShawngovsCodeExam.vscode
DAP server listening at: 127.0.0.1:60824
Build Error: go build -o C:UsersShawngovsCodeExam.vscode__debug_bin.exe -gcflags all=-N -l .
go: no modules were found in the current workspace; see 'go help work' (exit status 1)

I commented out both of the ("program":…) each time.(meaning running debug with both ("program":…) separately but NO luck no matter what the value of ("program":…) is, I get the error shown above.

I am still reading more about debugging in vscode but nothing points out which modules are missing?
or what the problem might be besides the modules.

2

Answers


  1. It is not possible to "debug a file" in isolation; there needs to be some sort of executable that calls the code being debugged. That is either a test or a program that the code is a part of (either an actual program or some form of test harness).

    So a lot hinges on what you mean by "debug a .go file".

    If that file is part of a test then you don’t need a launch file at all; you can use the go test integration provided with the Go extension for VS Code to debug the tests in the (test) file or individual tests.

    If the (code in the) file is not covered by any test then you will need a launch file that identifies a folder that is a module with a main() func that exercises the code you are trying to debug.

    It is my experience that having tests is the easiest way to perform ad-hoc, interactive debugging of go code (and equally, having tests means having to resort to interactive debugging less often!)

    Login or Signup to reply.
  2. thanks for the in put.
    I am totally newbie to Go (2 months in).
    Reading and doing all the exercises in the: https://code.visualstudio.com/docs/nodejs/nodejs-tutorial

    The .go file I mentioned above is very simple from mentioned tutorial.

    var msg = ‘Hello World’;
    console.log(msg);

    The file running just fine when I do: (node app.js) but for my own learning purposes on how to work with VSCode debugger tool, I add to this 2line code, my own logic like: ifelse statement and trying to learn debugging skills.

    So I understand that main() is a execution point for all apps.
    I’ll try to copypast any main.go file in the same folder as app.js or maybe add a ‘package main’ to the beginning of the file I am fiddling around.
    Tnx

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search