-2

I am starting to learn Golang.

I am coming from Python, a language I used almost exclusively. While learning Python, I felt having the ability to run code line by line in a Pycharm Python console extremely helpful in learning the language. For example, If a code block contained a variable, such as a dictionary, I could run that code block without having to run the entire script, and examine variable and it's contents within the console, as displayed in this image:

Python Console

Does a similar functionality exist for Golang? I am currently utilizing "Goland," an IntelliJ product.

Nate
  • 136
  • 10

1 Answers1

0

Since go is a compiled language you can't run just a block of it, but you can use real-time debugging, you can set breakpoints and inspect variables in real-time.

I can tell you the debug process in VS Code, but something similar is available in Goland too.

For VS Code you have to make a simple json file, called launch.json in which you tell the debugger how to run your process:

.vscode\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",
            "type": "go",
            "request": "launch",
            "mode": "exec",
            "program": "${workspaceFolder}/build/server.exe",
            "cwd": "${workspaceFolder}/build",
            "env": {},
            "args": []
        }
    ]
}

Then build a debug version of your app, where you turn of optimizations:

go build -v -gcflags=all=-l -o .\build\server.exe

Then in VS Code hit F5 to run your app in debug mode.

Fenistil
  • 3,734
  • 1
  • 27
  • 31