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.7k Golang : Find duplicate files with filepath.Walk
+37.7k Golang : Comparing date or timestamp
+6.5k Golang : Combine slices of complex numbers and operation example
+7k Golang : Find the shortest line of text example
+5.6k Python : Print unicode escape characters and string
+16.4k CodeIgniter/PHP : Create directory if does not exist example
+16.6k Golang : Delete files by extension
+39k Golang : How to iterate over a []string(array)
+29.5k Golang : How to create new XML file ?
+11k Golang : Create S3 bucket with official aws-sdk-go package
+7.1k Golang : A simple forex opportunities scanner
+16.8k Golang : Read integer from file into array