vvakame / astcopy

Package astcopy implements Go AST deep copy operations.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Go Report Card GoDoc Build Status

astcopy

Package astcopy implements Go AST reflection-free deep copy operations.

Installation:

go get github.com/go-toolsmith/astcopy

Example

package main

import (
	"fmt"
	"go/ast"
	"go/token"

	"github.com/go-toolsmith/astcopy"
	"github.com/go-toolsmith/astequal"
	"github.com/go-toolsmith/strparse"
)

func main() {
	x := strparse.Expr(`1 + 2`).(*ast.BinaryExpr)
	y := astcopy.BinaryExpr(x)
	fmt.Println(astequal.Expr(x, y)) // => true

	// Now modify x and make sure y is not modified.
	z := astcopy.BinaryExpr(y)
	x.Op = token.SUB
	fmt.Println(astequal.Expr(y, z)) // => true
	fmt.Println(astequal.Expr(x, y)) // => false
}

About

Package astcopy implements Go AST deep copy operations.

License:MIT License


Languages

Language:Go 100.0%