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.2k Linux : How to install driver for 600Mbps Dual Band Wifi USB Adapter
+5.4k Golang *File points to a file or directory ?
+21.8k SSL : How to check if current certificate is sha1 or sha2
+9.1k Golang : Intercept and compare HTTP response code example
+11.2k CodeIgniter : How to check if a session exist in PHP?
+7.3k Golang : Calculate how many weeks left to go in a given year
+8.1k Golang : Tell color name with OpenCV example
+8.7k Golang : Combine slices but preserve order example
+6.9k Golang : Normalize email to prevent multiple signups example
+7k Web : How to see your website from different countries?
+7.5k Golang : Dealing with struct's private part
+15k Golang : Search folders for file recursively with wildcard support