Golang go/ast.ExprStmt type, End() and Pos() functions examples
package go/ast
An ExprStmt node represents a (stand-alone) expression in a statement list.
Golang go/ast.ExprStmt type, End() and Pos() functions 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.ExprStmt:
return "expression statement"
...
}
Example 2:
func checkCurrentPosition(node ast.Node) string {
n := node.(*ast.ExprStmt)
endpos := n.End()
pospos := n.Pos()
if pospos != endpos {
return "not yet"
}
}
Example 3:
func createItStatementForTestFunc(testFunc *ast.FuncDecl) *ast.ExprStmt {
blockStatement := &ast.BlockStmt{List: testFunc.Body.List}
fieldList := &ast.FieldList{}
funcType := &ast.FuncType{Params: fieldList}
funcLit := &ast.FuncLit{Type: funcType, Body: blockStatement}
testName := rewriteTestName(testFunc.Name.Name)
basicLit := &ast.BasicLit{Kind: 9, Value: fmt.Sprintf("\"%s\"", testName)}
itBlockIdent := &ast.Ident{Name: "It"}
callExpr := &ast.CallExpr{Fun: itBlockIdent, Args: []ast.Expr{basicLit, funcLit}}
return &ast.ExprStmt{X: callExpr} // <-- here
}
References :
https://github.com/onsi/ginkgo/blob/master/ginkgo/convert/ginkgoastnodes.go
http://golang.org/pkg/go/ast/#ExprStmt
Advertisement
Something interesting
Tutorials
+13.5k Golang : How to get year, month and day?
+31.9k Golang : Convert an image file to []byte
+8.8k Android Studio : Image button and button example
+25.7k Golang : How to write CSV data to file
+13.2k Golang : Convert(cast) int to int64
+8.5k Golang : How to check if input string is a word?
+10.3k Golang : Wait and sync.WaitGroup example
+6.5k Golang : Spell checking with ispell example
+21.9k Golang : Use TLS version 1.2 and enforce server security configuration over client
+14.9k Golang : Basic authentication with .htpasswd file
+15.2k JavaScript/JQuery : Detect or intercept enter key pressed example
+5.4k Golang : Intercept, inject and replay HTTP traffics from web server