AST: Understanding the differences between functions and methods

117 views
Skip to first unread message

Tajmeet Singh

unread,
Sep 18, 2021, 2:20:19 PM9/18/21
to golang-nuts
Hi,

I was working on a tool for internal use where I had to work with the go's abstract syntax tree specially with the FuncDecls. I wanted to filter the ast to only function declarations without methods. I was wondering if there is a way to distinguish between normal functions and structure methods. I'm sure there must be a way since the godoc distinguishes them but don't know if they had to implement it in a certain way.

Currently my code uses AST's inspect function and traverses the tree. Example, 

```Go
ast.Inspect(syntaxtTree,  func(n *ast.Node) bool {
    ...
    switch x := n.(type) {
    case *ast.FuncDecl:
        ...    
}
```

If anyone knows a way to distinguish these, please do let me know. Thanks!

Sean Liao

unread,
Sep 18, 2021, 2:23:17 PM9/18/21
to golang-nuts
I believe the Recv field does what you want?

Tajmeet Singh

unread,
Sep 18, 2021, 7:37:37 PM9/18/21
to golang-nuts
Oh yes! Thank you so much :)
Reply all
Reply to author
Forward
0 new messages