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
+9.6k Golang : How to generate Code 39 barcode?
+5.7k Golang : Error handling methods
+9.5k Golang : Convert(cast) string to int64
+6.8k Golang : Calculate pivot points for a cross
+18.1k Golang : Convert IPv4 address to decimal number(base 10) or integer
+12.2k Golang : Get remaining text such as id or filename after last segment in URL path
+6.6k Golang : How to validate ISBN?
+6.5k Golang : Spell checking with ispell example
+14.4k Golang : Parsing or breaking down URL
+10.4k Golang : Meaning of omitempty in struct's field tag
+31.7k Golang : How to convert(cast) string to IP address?
+6k Javascript : Get operating system and browser information