Home > Back-end >  Golang debug shows a ~r2 variable which I haven't defined in my code
Golang debug shows a ~r2 variable which I haven't defined in my code

Time:10-27

I have a simple golang program, when I run it in debug mode, there is a ~r2 variable which I haven't defined and I don't know its use case, the content of this variable is constant unless I change something inside the code.

Here is the code:

package main

import "fmt"

func removeElement(nums []int, val int) int {
    if len(nums) > 0 {
        i := 0
        for {
            if nums[i] == val {
                nums = append(nums[:i], nums[i 1:]...)
            } else {
                i  
            }
            if i == len(nums) {
                break
            }
        }
    }
    return len(nums)
}

func main() {
    fmt.Println(removeElement([]int{0, 1, 2, 2, 3, 0, 4, 2}, 2))
}

Here is the variables in the vscode run and debug: enter image description here

CodePudding user response:

The ~r2 variable contains the values of unnamed return values of the function, which in your case an int. If you have more unnamed return values they will be ~r3 ...

  • Related