Golang go/parser.ParseExpr() function example
package go/parser
ParseExpr is a convenience function for obtaining the AST of an expression x. The position information recorded in the AST is undefined. The filename used in error messages is the empty string.
Golang go/parser.ParseExpr() function usage example
var TypeExprs = map[string]TypeExpr{
"int": TypeExpr{"int", "", 0, true},
"*int": TypeExpr{"*int", "", 1, true},
"[]int": TypeExpr{"[]int", "", 2, true},
"...int": TypeExpr{"[]int", "", 2, true},
"[]*int": TypeExpr{"[]*int", "", 3, true},
"...*int": TypeExpr{"[]*int", "", 3, true},
"MyType": TypeExpr{"MyType", "pkg", 0, true},
"*MyType": TypeExpr{"*MyType", "pkg", 1, true},
"[]MyType": TypeExpr{"[]MyType", "pkg", 2, true},
"...MyType": TypeExpr{"[]MyType", "pkg", 2, true},
"[]*MyType": TypeExpr{"[]*MyType", "pkg", 3, true},
"...*MyType": TypeExpr{"[]*MyType", "pkg", 3, true},
}
for typeStr, expected := range TypeExprs {
// Handle arrays and ... myself, since ParseExpr() does not.
array := strings.HasPrefix(typeStr, "[]")
if array {
typeStr = typeStr[2:]
}
ellipsis := strings.HasPrefix(typeStr, "...")
if ellipsis {
typeStr = typeStr[3:]
}
expr, err := parser.ParseExpr(typeStr) // <-- here
if err != nil {
t.Error("Failed to parse test expr:", typeStr)
continue
}
References :
https://github.com/revel/revel/blob/master/harness/reflect_test.go
Advertisement
Something interesting
Tutorials
+6.3k Golang : Extract sub-strings
+43.5k Golang : Get hardware information such as disk, memory and CPU usage
+10.6k Golang : Flip coin example
+13.6k Golang : Query string with space symbol %20 in between
+7.5k Golang : Rot13 and Rot5 algorithms example
+27.2k Golang : Find files by name - cross platform example
+5k Golang : Constant and variable names in native language
+6.8k Golang : Find the longest line of text example
+16.4k Golang : How to implement two-factor authentication?
+24.5k Golang : GORM read from database example
+15.7k Golang : Get checkbox or extract multipart form data value example
+30.5k Golang : Generate random string