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

http://golang.org/pkg/go/token/#Token.Lookup

Advertisement