Can I modify an ast node? Like AST rewrite functionality

(Sorry for the previous blunder).
Is there a way to rewrite an AST node in Go? Is there any function in Go that can do that?

I need this to remove unnecessary things from code. Like for example:

x := true
y := false

if y || x {
	fmt.Println("test 1")
}

I want to change this too:

fmt.Println("test 1")

There is this Github repo where I think I can do this. There was this issue on github to add this functionality for this. Is this resolved now?

And can somebody help how I can do this if a function exists to change the node?

Give it a shot! It looks like it would do what you want. The Github issue you linked to resulted in an implementation you can find here: https://godoc.org/golang.org/x/tools/go/ast/astutil

1 Like

Also, Can I delete a node in ast without losing its children(making them connect to their grandparent just like a simple tree operation)?

Thanks, I think I can do that using astutil

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.