golang中x.(type)只能在switch中使用

例如:

func MyPrintf(args ...interface{}) {
for _, arg := range args {
switch arg.(type) {
case int:
fmt.Println(arg, "is an int value.")
case string:
fmt.Println(arg, "is a string value.")
case int64:
fmt.Println(arg, "is an int64 value.")
default:
fmt.Println(arg, "is an unknown type.")
}
}
}


要从Go语言的抽象语法树提取实现了某个接口的结构体,可以按照以下步骤进行: 1. 使用Go语言自带的"go/ast"包,将Go代码解析为抽象语法树。 2. 遍历抽象语法树,找到所有的结构体定义。 3. 对于每个结构体定义,遍历其所有的方法,判断其是否有实现了目标接口的方法,若有则将该结构体记录下来。 4. 统计所有记录的结构体,即可得到实现了目标接口的结构体数量。 下面是一个简单的示例代码,用于提取实现了"io.Reader"接口的结构体数量: ```go package main import ( "fmt" "go/ast" "go/parser" "go/token" "os" ) func main() { // 读取Go代码文件 fset := token.NewFileSet() f, err := parser.ParseFile(fset, "example.go", nil, 0) if err != nil { fmt.Println(err) return } // 遍历抽象语法树,查找实现了"io.Reader"接口的结构体 count := 0 ast.Inspect(f, func(node ast.Node) bool { switch n := node.(type) { case *ast.InterfaceType: // 找到目标接口 if isIoReader(n) { // 遍历所有结构体定义 for _, d := range f.Decls { if gd, ok := d.(*ast.GenDecl); ok && gd.Tok == token.TYPE { for _, spec := range gd.Specs { if ts, ok := spec.(*ast.TypeSpec); ok { if st, ok := ts.Type.(*ast.StructType); ok { // 遍历结构体的所有方法,查找是否有实现了目标接口的方法 for _, f := range st.Fields.List { if len(f.Names) > 0 { name := f.Names[0] if isIoReaderMethod(f.Type, name.Name) { count++ fmt.Println("Found struct implementing io.Reader:", ts.Name.Name) } } } } } } } } } } return true }) fmt.Println("Total number of structs implementing io.Reader:", count) } // 判断一个类型是否是"io.Reader"接口类型 func isIoReader(t ast.Expr) bool { if ident, ok := t.(*ast.Ident); ok { if ident.Name == "Reader" && ident.Obj == nil { if sel, ok := ident.X.(*ast.SelectorExpr); ok { if pkg, ok := sel.X.(*ast.Ident); ok { if pkg.Name == "io" && pkg.Obj == nil { return true } } } } } return false } // 判断一个结构体方法是否实现了"io.Reader"接口的方法 func isIoReaderMethod(t ast.Expr, name string) bool { if sel, ok := t.(*ast.SelectorExpr); ok { if ident, ok := sel.X.(*ast.Ident); ok { if ident.Name == "io" { if sel.Sel.Name == "Read" && name == "Read" { return true } } } } return false } ``` 在上面的示例代码,我们使用了"go/ast"包的"Inspect"函数来遍历抽象语法树。在遍历过程,我们首先找到了目标接口"io.Reader",然后遍历所有的结构体定义,查找其是否有实现了目标接口的方法。如果找到了这样的结构体,就将其记录下来,并最终统计所有记录的结构体数量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值