How do I debug Go dependency packages?

Viewed 3774

Say my Go project depends on package example.com/foo. I am using Go 1.12, so the dependency is automatically pulled in by Go modules. That dependency has some problems/bugs, I want to add logs in the source code.

I can find the source code of the dependency on GitHub but I don't know how to make it into my project for debugging purpose.

3 Answers

First fetch all the dependency packages into the vendor folder.

go mod vendor

Then, change the source code in that and build your project by specifying to look into vendor folder.

go build -mod=vendor

or

go run -mod=vendor myapp.go

You can use replace directive:

replace example.com/original/import/path => /your/forked/import/path

Go module fetches packages into $GOPATH/pkg/mod you can change the source code there or using the vendor option of go mod to pull the packages into vendor folder and then start coding there.

Related