Golang go/token.Token.Lookup function example
package go/token
Lookup maps an identifier to its keyword token or IDENT (if not a keyword).
Golang go/token.Token.Lookup function usage example
// cmdName uses a reflect.Value of a Command to find a command's name.
// A command name can either be specified in a struct tag of a 'name' field,
// or if that is absent, the name of the struct type itself is used.
func cmdName(val reflect.Value) string {
mustBeStruct(val)
typ := val.Type()
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
tag := string(field.Tag)
if field.Name == "name" &&
field.Type.Kind() == reflect.String &&
token.Lookup(tag) == token.IDENT { // <-- here
return tag
}
}
return typ.Name()
}
References :
https://github.com/BurntSushi/gribble/blob/master/command_struct.go
Advertisement
Something interesting
Tutorials
+7.3k Golang : Fixing Gorilla mux http.FileServer() 404 problem
+8.4k Golang : Convert word to its plural form example
+15.6k Golang : rune literal not terminated error
+8.1k Golang : Check from web if Go application is running or not
+7.4k Android Studio : How to detect camera, activate and capture example
+7.4k Golang : How to detect if a sentence ends with a punctuation?
+33.8k Golang : convert(cast) bytes to string
+22.7k Golang : Set and Get HTTP request headers example
+80.7k Golang : How to return HTTP status code?
+9.7k Golang : Load ASN1 encoded DSA public key PEM file example
+18.5k Golang : Send email with attachment
+12.2k Golang : calculate elapsed run time