duanlu9816 2018-06-30 22:56
浏览 92
已采纳

Goland调试器的问题

I have got a strange bug in Golang.

OS: Linux Ubuntu 18.04.

CPU: AMD with 64 Bit Support.

IDE is Goland 2018.1.5.

Go version is 1.10.1.

Compiler is set to: 'Any'.

I have tried both 'gc' and 'gccgo' compilers. Result is the same.

In the program below, Debugger shows strange things. The 'aUnion' variable has "John" inside it, but the 'aRecord' variable which has a Union type in it, has no "John" inside.

If i 'fmt.Printf' them, they are both there, but Debugger shows no John inside 'aRecord'. Is that a debugger's bug?

The program is very simple. Just nested structs passed as a pointer.

    // 33.go.

    package main

    import "fmt"

    type Person struct {
        Name string
        Age  int
    }
    type GroupOfPeople struct {
        Name   string
        People []*Person
    }
    type Union struct {
        ID    int
        Group *GroupOfPeople
    }
    type Record struct {
        UnionField *Union
        Type       int
    }

    func main() {

        var aPerson *Person
        var people []*Person
        var aGroup *GroupOfPeople
        var aUnion *Union
        var aRecord *Record

        aPerson = &Person{
            Name: "John",
            Age:  10,
        }
        people = []*Person{aPerson}
        aGroup = &GroupOfPeople{
            Name:   "A Group",
            People: people,
        }
        aUnion = &Union{
            ID:    123,
            Group: aGroup,
        } // John is inside 'aUnion'.
        aRecord = &Record{
            UnionField: aUnion,
            Type:       666,
        }
        // John is NOT inside 'aRecord'.
        // WHY ?!
        fmt.Printf("aUnion: %+v.
", aUnion.Group.People[0])
        fmt.Printf("aRecord: %+v.
", aRecord.UnionField.Group.People[0])
    }

Thank you for help!

  • 写回答

1条回答 默认 最新

  • doujiaozhan2413 2018-07-01 13:15
    关注

    This is a problem in how GoLand handles the nesting for the values of these variables at runtime.

    I created this issue to track and fix it.

    Meanwhile, you can use the "Watches" functionality to watch for "aRecord.UnionField.Group.People" as an example, which will then correctly display the values. Sorry for the inconvenience.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?