hc ch
hc ch

Reputation: 137

How to inspect global variables in GoLand when debugging a golang program?

When I debug a go program in GoLand, I can't see the value of global variable. Can anyone tell me why and how to fix it?

Here is an example:

I set a breakpoint in the last line of main function. But as shown in the picture, we can only get the value of 'a', but not 'a' and 'xx'.

package main

import "fmt"

var xx int = 1

func main() {
    var a int = 1
    fmt.Println(a)
    xx = 3
    fmt.Println("end")
}

enter image description here

Upvotes: 4

Views: 4882

Answers (5)

rgoel
rgoel

Reputation: 560

I think there is no direct way. Just to elaborate more, you can click on Evaluate from Debug window: Evaluate Expression Button in Debug Window

Start typing the name of the variable, GoLand will try to autocomplete: Expression Window

Then click on Evaluate button: Autocompletion in Expression window

Upvotes: 0

Gakio
Gakio

Reputation: 1063

You can also right click the variable in the editor and "Add to Watches".

Upvotes: 2

Srikanth Lankapalli
Srikanth Lankapalli

Reputation: 136

visual code will show both local and global variables.

Upvotes: 0

hc ch
hc ch

Reputation: 137

We can use Evaluate function to get the value of global variables, but can only get one value each time

evaluate

Upvotes: 3

Arman Ordookhani
Arman Ordookhani

Reputation: 6556

I think there is no way to show all global variables automatically. Just add them manually (by clicking plus sign or pressing Insert).

Upvotes: 2

Related Questions