Golang go/ast.BlockStmt type examples

package go/ast

A BlockStmt node represents a braced statement list.

Golang go/ast.BlockStmt type usage examples

Example 1:

 func NodeDescription(n ast.Node) string {
 switch n := n.(type) {
 case *ast.ArrayType:
 return "array type"
 case *ast.AssignStmt:
 return "assignment"
 case *ast.BlockStmt:
 return "block"
 ...
 }

Example 2:

 var node ast.Node
 ...
 switch n := node.(type) {
 var stmt := n.Else.(type)

 block := &ast.BlockStmt{
 Lbrace: n.Body.End(), // Start at end of the "if" block so the covered part looks like it starts at the "else".
 List: []ast.Stmt{stmt},
 Rbrace: stmt.End(),
 }
 n.Else = block
 }
 ...

Reference :

http://golang.org/pkg/go/ast/#BlockStmt

Advertisement