Ah, found it!  If I write types.NewPointer(reader.Type()) instead of just 
reader.Type(), things work better.

On Tuesday, 18 April 2023 at 20:34:30 UTC+1 Jochen Voss wrote:

> Dear all,
>
> I'm trying to find the callgraph of a method, using the following code:
>
> package main
>
> import (
> "log"
>
> "golang.org/x/tools/go/callgraph"
> "golang.org/x/tools/go/packages"
> "golang.org/x/tools/go/ssa/ssautil"
> )
>
> func main() {
> cfg := &packages.Config{Mode: packages.LoadSyntax}
> initial, err := packages.Load(cfg, "seehuhn.de/go/pdf")
> if err != nil {
> log.Fatal(err)
> } else if packages.PrintErrors(initial) > 0 {
> log.Fatalf("packages contain errors")
> }
>
> prog, pkgs := ssautil.Packages(initial, 0)
> if len(pkgs) != 1 {
> log.Fatalf("expected 1 package, got %d", len(pkgs))
> }
> pkg := pkgs[0]
> pkg.Build()
>
> reader := pkg.Members["Reader"]
>
> f := prog.LookupMethod(reader.Type(), pkg.Pkg, "readXRef")
> g := callgraph.New(f)
> _ = g
> }
>
> (Also on the playgound at https://go.dev/play/p/_S-oKAJ6YNO, but 
> resulting in some fancy error messages there.)
>
> This fails with the following error message: panic: 
> seehuhn.de/go/pdf.Reader has no method seehuhn.de/go/pdf.readXRef
>
> What am I doing wrong?
>
> All the best,
> Jochen
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/c128c8fb-bb40-4cbe-86a1-f6da0c88a78bn%40googlegroups.com.

Reply via email to