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
+6.7k Golang : Output or print out JSON stream/encoded data
+9.4k Golang : Web(Javascript) to server-side websocket example
+9.8k Golang : Format strings to SEO friendly URL example
+5.4k Unix/Linux/MacOSx : How to remove an environment variable ?
+14.4k Android Studio : Use image as AlertDialog title with custom layout example
+8.1k Golang : Variadic function arguments sanity check example
+7.4k Golang : Accessing dataframe-go element by row, column and name example
+8.4k Your page has meta tags in the body instead of the head
+4.9k Unix/Linux : secure copying between servers with SCP command examples
+30k Golang : How to declare kilobyte, megabyte, gigabyte, terabyte and so on?
+25.7k Golang : missing Mercurial command
+7.8k Swift : Convert (cast) String to Double