Golang package that find original source path
package main
import (
"fmt"
"github.com/shibukawa/findpackagesrc"
)
func main() {
finder, err := findpackagesrc.NewFinder(findpackagesrc.Option{})
if err != nil {
panic(err)
}
path, err := finder.FindSourcePath("github.com/stretchr/testify")
if err != nil {
panic(err)
}
fmt.Println(path)
// /home/yourname/go/pkg/mod/github.com/stretchr/testify@v1.4.0
}
It searches in the following order (I don't know the order is as same as Go's rule):
- Replace defined in go.mod
- Downloaded by go mod in go.sum
- vendor folder
- $GOPATH
- $GOROOT (for go standard library)
Apache 2