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
+32.7k Golang : Regular Expression for alphanumeric and underscore
+13.8k Golang : Gin framework accept query string by post request example
+21.2k Golang : How to get time zone and load different time zone?
+5.3k Golang : Get FX sentiment from website example
+9.4k Golang : Scramble and unscramble text message by randomly replacing words
+8.1k Golang : How To Use Panic and Recover
+11.6k Golang : Display a text file line by line with line number example
+4.7k Chrome : How to block socketloop.com links in Google SERP?
+9.7k Golang : Sort and reverse sort a slice of floats
+12.6k Golang : Get absolute path to binary for os.Exec function with exec.LookPath
+14.6k Golang : Convert(cast) int to float example
+14k Golang : Compress and decompress file with compress/flate example