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
+80.7k Golang : How to return HTTP status code?
+7.7k Golang : get the current working directory of a running program
+7.8k Golang : Example of how to detect which type of script a word belongs to
+22.2k Golang : How to run Golang application such as web server in the background or as daemon?
+17.9k Golang : How to make a file read only and set it to writable again?
+8.1k Golang : Variadic function arguments sanity check example
+10.8k PHP : Convert(cast) bigInt to string
+15.4k Golang : invalid character ',' looking for beginning of value
+17.2k Google Chrome : Your connection to website is encrypted with obsolete cryptography
+18.8k Golang : Delete duplicate items from a slice/array
+10.3k Golang : Wait and sync.WaitGroup example
+13.7k Golang : Image to ASCII art example