解析go src,尝试将* ast.GenDecl转换为types.Interface

I am trying to parse go source files that contain interfaces and find the interfaces defined methods / signatures. I am using ast to parse the file. I am able to get some high level declarations, such as *ast.GenDecl, but I am not able to get to the next level of determining if this type is an interface and what its methods are.

This is for a scaffolding type problem I am trying to solve where a user defines the interface for a service and a tool will build out the skeleton of the service

package main

import (
        "fmt"
        "go/ast"
        "go/parser"
        "go/token"
        "reflect"
)

func main() {
        fset := token.NewFileSet()
        f, _ := parser.ParseFile(fset, "/tmp/tmp.go", `package service

        type ServiceInterface interface {
                        Create(NewServiceRequest) (JsonResponse, error)
                        Delete(DelServiceRequest) (JsonResponse, error)
        }`, 0)
        for _, v := range f.Decls {

            switch t := v.(type) {
            case *ast.FuncDecl:
                    fmt.Println("func ", t.Name.Name)
            case *ast.GenDecl:
                    switch x := t.Specs[0].(type) {
                    default:
                            fmt.Println(x, reflect.TypeOf(x))
                    }
            default:
                    fmt.Printf("skipping %t
", t)
            }

    }
}

Results in but I cant seem to find anything about the internals of interface declaration at all.

&{<nil> ServiceInterface 0x8202d8260 <nil>} *ast.TypeSpec

When working with the AST, I find it helpful to dump an example using the spew package:

    fset := token.NewFileSet()
    f, _ := parser.ParseFile(fset, "/tmp/tmp.go", `package service ....`)
    spew.Dump(f)

I find it easy to write the required code from the spew output.

Here's some code to get you started. It prints interface and method names:

package main

import (
  "fmt"
  "go/ast"
  "go/parser"
  "go/token"
)

func main() {
  fset := token.NewFileSet()
  f, _ := parser.ParseFile(fset, "/tmp/tmp.go", `package service

    type ServiceInterface interface {
                    Create(NewServiceRequest) (JsonResponse, error)
                    Delete(DelServiceRequest) (JsonResponse, error)
    }`, 0)
  for _, x := range f.Decls {
    if x, ok := x.(*ast.GenDecl); ok {
        if x.Tok != token.TYPE {
            continue
        }
        for _, x := range x.Specs {
            if x, ok := x.(*ast.TypeSpec); ok {
                iname := x.Name
                if x, ok := x.Type.(*ast.InterfaceType); ok {
                    for _, x := range x.Methods.List {
                        if len(x.Names) == 0 {
                            continue
                        }
                        mname := x.Names[0].Name
                        fmt.Println("interface:", iname, "method:", mname)

                    }
                }
            }
        }
    }
  }
}

http://play.golang.org/p/eNyB7O6FIc