go - 查找 *ast.FuncDecl 附加到的结构
问题描述
下面是Handler接口的简单实现:
type someApi struct {
mu *sync.RWMutex
}
func (api *someApi) ServeHTTP(w http.ResponseWriter, r *http.Request) {}
func NewSomeApi(mu *sync.RWMutex) *someApi {
return &someApi{
mu: mu,
}
}
func (srv *someApi) Create() {
// some realisation
}
func Create() {
// some realisation
}
我想要解析文件go/ast
并为函数创建装饰器someApi.Create
。用 获取 func 名称很简单*ast.FuncDecl.Name
,但是我怎样才能找到Create
附加的 funcsomeApi
呢?
解决方案
通过迭代来解决这个问题*ast.FuncDecl.Recv.List
:
for _, l := range fn.Recv.List { // fn is *ast.FuncDecl
star, ok := l.Type.(*ast.StarExpr)
if !ok {
continue
}
fmt.Println(star.X) // someApi
}
推荐阅读
- python - Python:下载基于名称和日期的 s3 文件
- javascript - 使用 onclick 功能忽略阻止默认值
- javascript - 高阶函数中count++和count+1的区别
- python - 使用 Pandas 读取变量列标题时出现问题
- ios - 如何在 UICollectionView 的单元格上使用 TouchDragEnter?
- ios - 在 Objective-C 中设置一个类的值的问题
- javascript - 将javascript对象键与输入值进行比较
- c# - Set style with eventsetter in resources programmatically
- youtube - 从 Youtube 直播视频中获取当前延迟
- django - apache + mod_wsgi restart 保持活动任务