Finding imports and dependencies of a go program -
the go list -json
command run command line tell imports , dependencies of go program ( in json format). there way information within go program i.e @ runtime, either running 'go list' command somehow or way?
i don't think can without using go binary since go needs analyze source code.
it's pretty easy must have access go , source code @ run time. heres quick example:
package main import ( "encoding/json" "fmt" "os/exec" ) func main() { cmd := exec.command("go", "list", "-json") stdout, err := cmd.output() if err != nil { println(err.error()) return } var list golist err = json.unmarshal(stdout, &list) _, d := range list.deps { fmt.printf(" - %s\n", d) } } type golist struct { dir string importpath string name string target string stale bool root string gofiles []string imports []string deps []string }
Comments
Post a Comment