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
+12.1k Golang : md5 hash of a string
+7.7k Golang : Test if an input is an Armstrong number example
+9.6k Golang : How to generate Code 39 barcode?
+5.3k PHP : Hide PHP version information from curl
+9.1k Golang : Serving HTTP and Websocket from different ports in a program example
+14.1k Golang : Check if a file exist or not
+7.8k Golang : Load DSA public key from file example
+16.9k Golang : Get the IPv4 and IPv6 addresses for a specific network interface
+5.7k List of Golang XML tutorials
+9k Golang : automatically figure out array length(size) with three dots
+6.5k Grep : How to grep for strings inside binary data