- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在尝试查找 func 参数类型及其名称以生成一些代码。我正在为它使用 ast
包。我找到了参数名称,但找不到参数类型名称。
for _, fun := range findFunc(node) { //ast.FuncDecl
if fun.Recv != nil {
for _, field := range fun.Type.Params.List {
for _, name := range field.Names {
println(name.Name) //Func field name
//How to find field type name here?
}
}
}
}
最佳答案
你走对了,FuncType.Params
是函数的(传入)参数列表。它的 List
字段包含所有参数,类型为 ast.Field
:
type Field struct {
Doc *CommentGroup // associated documentation; or nil
Names []*Ident // field/method/parameter names; or nil if anonymous field
Type Expr // field/method/parameter type
Tag *BasicLit // field tag; or nil
Comment *CommentGroup // line comments; or nil
}
Field.Type
保存接口(interface)类型的参数类型 ast.Expr
只需嵌入 ast.Node
它只为您提供源代码中类型的开始和结束位置。基本上这应该足以获取类型的文本表示。
让我们看一个简单的例子。我们将分析这个函数:
func myfunc(i int, s string, err error, pt image.Point, x []float64) {}
以及打印其参数(包括类型名称)的代码:
src := `package xx
func myfunc(i int, s string, err error, pt image.Point, x []float64) {}`
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "src.go", src, 0)
if err != nil {
panic(err)
}
offset := f.Pos()
ast.Inspect(f, func(n ast.Node) bool {
if fd, ok := n.(*ast.FuncDecl); ok {
fmt.Printf("Function: %s, parameters:\n", fd.Name)
for _, param := range fd.Type.Params.List {
fmt.Printf(" Name: %s\n", param.Names[0])
fmt.Printf(" ast type : %T\n", param.Type)
fmt.Printf(" type desc : %+v\n", param.Type)
fmt.Printf(" type name from src: %s\n",
src[param.Type.Pos()-offset:param.Type.End()-offset])
}
}
return true
})
输出(在 Go Playground 上尝试):
Function: myfunc, parameters:
Name: i
ast type : *ast.Ident
type desc : int
type name from src: int
Name: s
ast type : *ast.Ident
type desc : string
type name from src: string
Name: err
ast type : *ast.Ident
type desc : error
type name from src: error
Name: pt
ast type : *ast.SelectorExpr
type desc : &{X:image Sel:Point}
type name from src: image.Point
Name: x
ast type : *ast.ArrayType
type desc : &{Lbrack:71 Len:<nil> Elt:float64}
type name from src: []float64
关于Go lang func参数类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50524607/
我在等待异步功能完成时苦苦挣扎。特别是,我发现这两种方法在测试继续之前等待异步函数完成,但不知道其中的区别(如果有区别的话):。我的目标是在实际测试开始之前等待bepreEach()块中的两个异步函数
我在等待异步功能完成时苦苦挣扎。特别是,我发现这两种方法在测试继续之前等待异步函数完成,但不知道其中的区别(如果有区别的话):。我的目标是在实际测试开始之前,在beforeEach()块中等待两个Ja
为什么是Func<>从 Expression> 创建通过 .Compile() 比仅使用 Func<> 慢得多直接声明? 我刚从使用 Func 更改为直接声明为从 Expression> 创建的一个在
我正在创建一个 Validator类(class)。我正在尝试实现 Linq SelectMany我的验证器的扩展方法能够使用 Linq 查询组合表达式并验证最终结果,即使基础值发生变化也是如此。 下
function sum(a) { let currentSum = a; function f(b) { currentSum += b; return f; }
我只知道i = i++;是未定义的行为,但是如果一个表达式中调用了两个或多个函数,并且所有功能是一样的。是未定义吗?例如: int func(int a) { std::cout << a <
我如何定义一个对象,以便作用于它的任何函数都作用于它的一个字段?这可能吗? class Mydata(object): def __init__(self, val): sel
这个问题一直很有趣,尽管它不一定很整洁。我有以下代码: import random def d(m): return random.randint(1, m) print(3*d(6)) 这将
能否请您解释一下使用 func.apply(null, arr) 的区别?和 func.apply(this, arr)在下面的代码示例中? var Foo = function() { fu
我想收集/运行任务,然后对它们执行 Task.WhenAll。 var tasks = new List(); foreach (var thing in things) { tasks.Add(
我有以下代码: static Func s_objToString = (x) => x.ToString(); static Func s_stringToString = s_objToStrin
相关主题: Create Expression> dynamically 我在互联网上搜索但所有样本都解释了 Expression来自 T ? 谢谢 编辑 1) T输入我的代码在运行时确定,例如我想用
我正在尝试使用 LinqKit 动态生成 linqtosql 查询.在将表达式发送到 LinqKit 之前,我想检查要为预测添加的字段。所以我想出了一些想法,比如 Expression> GetPr
我遇到了一些麻烦,我写了一个 Func,IDE 不喜欢我在 Func 体内调用 Func ,我不太明白为什么,因为如果我将这个确切的代码放在方法体中,并使用相同的返回类型和参数,那么它就可以工作。 代
我现在正在学习使用 Class 语法来创建 React 组件,请注意我现在必须声明这样的方法: class Foo extends React.Component { ... bar
下面两种说法有区别吗?他们都工作。 if ( ((Func)(()=>true))() ) { .... }; if ( new Func(()=>true)()) { .... }; 最佳答案 不,
这个问题在这里已经有了答案: Difference between func() and (*this).func() in C++ (4 个答案) 关闭 6 年前。 如果我有一个带有虚函数而没有自
主要问题是“是否可以将任何类型的 func 作为参数传递以及如何传递?”。我正在学习 Go 并且想像这样制作我自己的异步包装函数: func AsyncFunc(fn func(), args ...
有没有简单的转换方法 Expression> 到 Expression> T从哪里继承自TBase? 最佳答案 只要 T 派生自 TBase,您就可以使用原始表达式的主体和参数直接创建所需类型的表达式
我有以下方法,其中 T 在 Func 中使用: public void DoSomething(string someString, Func someMethod) { if(some
我是一名优秀的程序员,十分优秀!