package main import ( "fmt" "go/types" "golang.org/x/tools/go/ssa" "golang.org/x/tools/go/ssa/interp" ) func main() { var prog *ssa.Program eval := interp.New(prog) val := types.Const{Value: types.Int64Val(42)} result, _ := eval.ValueOf(&val) fmt.Println(result) }
package main import ( "go/types" "golang.org/x/tools/go/ssa" "golang.org/x/tools/go/ssa/ssautil" ) func main() { var prog *ssa.Program funcs := ssautil.AllFunctions(prog) for _, fn := range funcs { for _, b := range fn.Blocks { for _, instr := range b.Instrs { switch instr := instr.(type) { case *ssa.BinOp: if instr.Op == token.ADD { x := instr.X.(*ssa.Const).Int64() y := instr.Y.(*ssa.Const).Int64() fmt.Printf("%d + %d = %d\n", x, y, x+y) } } } } } }This example uses the Value package to iterate over all the instructions in an SSA program and print the result of any binary addition operations that are found.