Golang go/ast.BasicLit type examples
package go/ast
A BasicLit node represents a literal of basic type.
Golang go/ast.BasicLit 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.BasicLit:
return "basic literal"
...
}
Example 2:
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)} //<-- here
itBlockIdent := &ast.Ident{Name: "It"}
callExpr := &ast.CallExpr{Fun: itBlockIdent, Args: []ast.Expr{basicLit, funcLit}}
return &ast.ExprStmt{X: callExpr}
}
Reference :
Advertisement
Something interesting
Tutorials
+8.4k Golang : Generate Datamatrix barcode
+11.4k Golang : Delay or limit HTTP requests example
+14.6k Golang : Missing Bazaar command
+8.8k Golang : Accept any number of function arguments with three dots(...)
+19.2k Golang : Delete item from slice based on index/key position
+8.2k How to show different content from website server when AdBlock is detected?
+6.9k Golang : Fibonacci number generator examples
+8.2k Golang : Routes multiplexer routing example with regular expression control
+34k Golang : Proper way to set function argument default value
+14.1k Javascript : Prompt confirmation before exit
+11.5k Golang : Generate DSA private, public key and PEM files example