Search code examples
godelve

dlv debug fail due to undefined object in same package


I filed a bug for this over at the delve site. So, to explain what's going on. I have 2 files in the same package, main.go and common.go. In main.go, it uses some structure from common.go and when i run

dlv debug --listen=:2345 --headless --api-version=2 --log main.go

it fails with 'undefined: NewSimpleStruct' and i am not sure what i am doing wrong.

Here's what the Go files contain,

//main.go
package main

import (
    "fmt"
)

func main() {
    fmt.Println("HELLO WORLD!")
    segasaturn := NewSimpleStruct("SS", 69)
    segasaturn.WhoAmI()

    fmt.Println("BYE WORLD!")
}


//common.go
package main

import "fmt"

type simpleStruct struct {
    name string
    id   int
}

func NewSimpleStruct(name string, id int) *simpleStruct {
    return &simpleStruct{name, id}
}

func (ss *simpleStruct) WhoAmI() {
    fmt.Printf("name: %s, id: %d\n", ss.name, ss.id)
}

Solution

  • A common error, and it's not delve's fault; note that you also can't go build main.go here.

    You need to do go build . or go build main.go common.go.

    Similarly just put all the files or a dot(.) instead of main.go to include all the .go files in the directory

    dlv debug --listen=:2345 --headless --api-version=2 --log .