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
+8.2k Golang : Reverse text lines or flip line order example
+19.5k Golang : How to Set or Add Header http.ResponseWriter?
+8.3k Golang : Oanda bot with Telegram and RSI example
+8.1k Golang : Get all countries phone codes
+5.2k Golang : Experimental Jawi programming language
+6.1k PageSpeed : Clear or flush cache on web server
+13.5k Facebook PHP getUser() returns 0
+5.4k Golang : Get S3 or CloudFront object or file information
+23.5k Golang : Check if element exist in map
+6.9k Fix sudo yum hang problem with no output or error messages
+22.7k Golang : Set and Get HTTP request headers example