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
+16.4k Golang : How to implement two-factor authentication?
+39.6k Golang : Remove dashes(or any character) from string
+11.7k Golang : Secure file deletion with wipe example
+17.1k Golang : XML to JSON example
+9.1k Golang : Gonum standard normal random numbers example
+8.2k Golang : Routes multiplexer routing example with regular expression control
+6k Fontello : How to load and use fonts?
+6.8k Golang : Join lines with certain suffix symbol example
+4k Detect if Google Analytics and Developer Media are loaded properly or not
+15.9k Golang : Read a file line by line
+25.6k Golang : convert rune to integer value