Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read input when debugging GO in Visual Studio Code?

I want to debug a Go program in Visual Studio Code 1.24.0 which goes like this:

package main

import (
   "fmt"
)

func main() {
 fmt.Println("Hello World")
 var input int
 fmt.Scanf("%d", &input)
 fmt.Printf("Hello %v", input)
}

When start debugging, the program waits for input. I tried giving input via Debug Console, but it didn't work. Properties like externalConsole don't seem to work in launch.json for Go. Any inputs?

like image 202
object Avatar asked Jun 16 '18 04:06

object


2 Answers

I'm happy to share that a more usable solution (It automatically runs the task before launching) is found.

...

launch.json:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Connect to server",
            "type": "go",
            "request": "attach",
            "preLaunchTask": "delve",
            "mode": "remote",
            "remotePath": "${workspaceFolder}",
            "port": 23456,
            "host": "127.0.0.1",
            "cwd": "${workspaceFolder}"
        }
    ]
}

And
tasks.json:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "delve",
            "type": "shell",
            "command": "dlv debug --headless --listen=:23456 --api-version=2 \"${workspaceFolder}\"",
            "isBackground": true,
            "presentation": {
                "focus": true,
                "panel": "dedicated",
                "clear": false
            },
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": {
                "pattern": {
                    "regexp": ""
                },
                "background": {
                    "activeOnStart": true,
                    "beginsPattern": {
                        "regexp": ".*"
                    },
                    "endsPattern": {
                        "regexp": ".*server listening.*"
                    }
                }
            }
        }
    ]
}

It expects you are working with a go.mod since delve also looks for it to debug the directory.

like image 139
Dan Ortega Avatar answered Sep 22 '22 17:09

Dan Ortega


In the same Issue HowieLiuX shared a sinmpler solution that works for me:

In tasks.json:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "cd ${fileDirname} && dlv debug --headless --listen=:2345 --log --api-version=2",
            "problemMatcher": [],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

I, however, had to change command to:

"command": "dlv debug --headless --listen=:2345 --log --api-version=2",

since I'm running through powershell.

Then in launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Connect to server",
            "type": "go",
            "request": "launch",
            "mode": "remote",
            "remotePath": "${fileDirname}",
            "port": 2345,
            "host": "127.0.0.1",
            "program": "${fileDirname}",
            "env": {},
            "args": []
        }
    ]
}

First run the task then debug.

like image 27
Cpt.Ohlund Avatar answered Sep 25 '22 17:09

Cpt.Ohlund