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
+11.2k Golang : Fix - does not implement sort.Interface (missing Len method)
+12.1k Golang : Perform sanity checks on filename example
+9.4k Android Studio : Indicate progression with ProgressBar example
+6.9k Golang : Fibonacci number generator examples
+30.5k Get client IP Address in Go
+15k Golang : package is not in GOROOT during compilation
+6k Golang : Convert Chinese UTF8 characters to Pin Yin
+5k Golang : Calculate a pip value and distance to target profit example
+20k Golang : Convert(cast) bytes.Buffer or bytes.NewBuffer type to io.Reader
+13.1k Golang : List objects in AWS S3 bucket
+31.7k Golang : How to convert(cast) string to IP address?
+3.6k Java : Get FX sentiment from website example