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
+10.2k Golang : Use regular expression to get all upper case or lower case characters example
+11.7k Golang : Find age or leap age from date of birth example
+5.4k Unix/Linux : How to archive and compress entire directory ?
+6.1k Golang : Missing Subversion command
+27.9k Golang : Decode/unmarshal unknown JSON data type with map[string]interface
+10.8k PHP : Convert(cast) bigInt to string
+6.3k Javascript : Generate random key with specific length
+8.5k Linux/Unix : fatal: the Postfix mail system is already running
+41.4k Golang : Convert string to array/slice
+7.7k Golang : Mapping Iban to Dunging alphabets
+19.2k Golang : Execute shell command
+15.4k Golang : invalid character ',' looking for beginning of value