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
+14.1k Javascript : Prompt confirmation before exit
+18.5k Golang : Set, Get and List environment variables
+6.5k Golang : Calculate diameter, circumference, area, sphere surface and volume
+5.4k Golang : Intercept, inject and replay HTTP traffics from web server
+4.9k Unix/Linux : secure copying between servers with SCP command examples
+10.6k Golang : How to unmarshal JSON inner/nested value and assign to specific struct?
+17.6k Golang : Parse date string and convert to dd-mm-yyyy format
+9.1k Golang : io.Reader causing panic: runtime error: invalid memory address or nil pointer dereference
+6.9k Golang : Decode XML data from RSS feed
+6.8k Swift : substringWithRange() function example
+38.1k Golang : Read a text file and replace certain words
+12.3k Golang : Validate email address