Rushing Labs

Setting up Delve for Golang Debugging, and a simple error

Working on gophercises, naturally I started with the first problem. I needed to read in this CSV and for each line (a) ask a question, (b) read in an answer, (c) record if it's correct, (d) keep track of the score across all questions.

This should be a good problem for learning some basics of the language.

5+5,10
7+3,10
1+1,2
/* removed for brevity */

First Issue: A string comparison doesn't work?§

This (seemingly simple) string comparison didn't seem to work. But, why?

We're creating a new reader to parse CLI input. And even removing the included \n character which is read in from the user input. So, why wasn't this simple comparison working?? (Keep this EOL character in mind. It will come back at the end of the article.)

inputReader := bufio.NewReader(os.Stdin)
input, err := inputReader.ReadString('\n')
if strings.Compare(input, records[i][1]) == 0 { // <- This is what was failing!
    fmt.Println("CORRECT!")
} else {
    fmt.Println("WRONG!")
}

So, we need to debug!

Delve, a debugger§

Ref: https://www.rookout.com/blog/golang-debugging-tutorial/

So, we need a debugger to inspect literal values...so we can see what trickery is at play.

The link above points me at Delve. This is a debugger for Go. But not so fast. Installing it only gives you a CLI debugger. Meaning you technically have a full debugger, but it's only available from the terminal. We really need this available from within VSCode.

Either way, install it like so.

go install github.com/go-delve/delve/cmd/dlv@latest

Configuring Delve + VSCode§

This is the default launch.json we're given. But it's wrong!

Trying to debug with this configuration gets us this weird error.

[ insert a screenshot of the strange error ]

{
    // 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": "Launch Package",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${fileDirname}"
        }
    ]
}

Solvin the 'tty' issue§

According to this GitHub issue, it happens because "VSCode's DEBUG CONSOLE does not have tty". That issue then refers us to the console property for the VSCode launch.json debugger configuration.

[ insert a screenshot of the GitHub issue ]

We need to set integratedTerminal for console. The default for console is internalConsole. So, be careful to make this change as _it is what forces our debug session to open in the VSCode's "Terminal" instead of the "Debug Console".

Ref, GitHub issue: https://github.com/golang/vscode-go/issues/2053#issuecomment-1031635355 Ref, launch.json console: https://github.com/golang/vscode-go/blob/master/docs/debugging.md#configuration

Solving the folder issue§

Then, because for some reason the default config is looking the wrong directory (it's not able to find our .go code), we need to set program to the workspace VSCode is looking at.

"program": "${workspaceFolder}"

Skip to the correct 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": "Launch Package",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${workspaceFolder}",
            "console": "integratedTerminal"
        }
    ]
}

The EOL character§

Then I saw that, because I already had some code to remove \n (assumed newline char), I still had an \r in my input variable. This little mixup is because I'm running this code on Windows, and the tutorial I was following was written for Linux.

[ insert screenshot of seeing this value in input ]

So, it's the right thought, but we have to be aware that Windows and Linux systems use different EOL characters.

Ref: https://tutorialedge.net/golang/reading-console-input-golang/ Ref: https://www.cs.toronto.edu/~krueger/csc209h/tut/line-endings.html