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
+15.6k Golang : rune literal not terminated error
+14.6k Golang : Send email with attachment(RFC2822) using Gmail API example
+21.2k Golang : Clean up null characters from input data
+19.2k Golang : Check if directory exist and create if does not exist
+10.2k Golang : Text file editor (accept input from screen and save to file)
+31.5k Golang : bufio.NewReader.ReadLine to read file line by line
+6.9k Mac OSX : Find large files by size
+23.9k Golang : Fix type interface{} has no field or no methods and type assertions example
+14.4k Golang : How to convert a number to words
+9.7k Golang : List available AWS regions
+48.1k Golang : How to convert JSON string to map and slice
+12.3k Golang : Print UTF-8 fonts on image example