gpt4 book ai didi

go - 从 Go 中的 AST 中删除节点

转载 作者:IT王子 更新时间:2023-10-29 01:47:59 28 4
gpt4 key购买 nike

有没有办法从 AST 中删除特定类型的节点? (例如,我想删除所有 Expr 语句)或者也许有办法用空的 ast.Expr 节点替换它?

我没有找到任何关于它的文档,所以任何帮助将不胜感激。提前致谢!

更新:

func find_node(file *ast.File) {

ast.Inspect(file, func(node ast.Node) bool {

switch x := node.(type) {

case *ast.ExprStmt:

var e ast.Expr
e = x.X

fmt.Printf("Expression found: %s\n", e)

}

return true
})

最佳答案

你可以试试 astutil包裹。请参阅 rewrite_test.go 测试中的删除示例.

关于go - 从 Go 中的 AST 中删除节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35609633/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com