本文整理汇总了Golang中reflect.Type类的典型用法代码示例。如果您正苦于以下问题:Golang Type类的具体用法?Golang Type怎么用?Golang Type使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Type类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: finishNewTable
func finishNewTable(b *tablebase, ty reflect.Type) Table {
id := C.newTable()
t := &table{
scroller: newScroller(id, true), // border on Table
tablebase: b,
selected: newEvent(),
}
t.fpreferredSize = t.xpreferredSize
// also sets the delegate
C.tableMakeDataSource(t.id, unsafe.Pointer(t))
for i := 0; i < ty.NumField(); i++ {
colname := ty.Field(i).Tag.Get("uicolumn")
if colname == "" {
colname = ty.Field(i).Name
}
cname := C.CString(colname)
coltype := C.colTypeText
editable := false
switch {
case ty.Field(i).Type == reflect.TypeOf((*image.RGBA)(nil)):
coltype = C.colTypeImage
case ty.Field(i).Type.Kind() == reflect.Bool:
coltype = C.colTypeCheckbox
editable = true
}
C.tableAppendColumn(t.id, C.intptr_t(i), cname, C.int(coltype), toBOOL(editable))
C.free(unsafe.Pointer(cname)) // free now (not deferred) to conserve memory
}
return t
}
示例2: addImportsForInterfaceMethods
// Add imports for each of the methods of the interface, but not the interface
// itself.
func addImportsForInterfaceMethods(imports importMap, it reflect.Type) {
// Handle each method.
for i := 0; i < it.NumMethod(); i++ {
m := it.Method(i)
addImportsForType(imports, m.Type)
}
}
示例3: sampleFormat
func sampleFormat(b reflect.Type) (f C.PaSampleFormat) {
if b.Kind() != reflect.Slice {
return 0
}
b = b.Elem()
if b.Kind() == reflect.Slice {
f = C.paNonInterleaved
b = b.Elem()
}
switch b.Kind() {
case reflect.Float32:
f |= C.paFloat32
case reflect.Int32:
f |= C.paInt32
default:
if b == reflect.TypeOf(Int24{}) {
f |= C.paInt24
} else {
return 0
}
case reflect.Int16:
f |= C.paInt16
case reflect.Int8:
f |= C.paInt8
case reflect.Uint8:
f |= C.paUInt8
}
return f
}
示例4: CopyTableToStruct
// Copy matching Lua table entries to a struct, given the struct type
// and the index on the Lua stack.
func CopyTableToStruct(L *lua.State, t reflect.Type, idx int) interface{} {
was_ptr := t.Kind() == reflect.Ptr
if was_ptr {
t = t.Elem()
}
s := reflect.New(t) // T -> *T
ref := s.Elem()
L.PushNil()
if idx < 0 {
idx--
}
for L.Next(idx) != 0 {
key := L.ToString(-2)
f := ref.FieldByName(strings.Title(key))
if f.IsValid() {
val := luaToGoValue(L, f.Type(), -1)
f.Set(val)
}
L.Pop(1)
}
if was_ptr {
return s.Interface()
}
return s.Elem().Interface()
}
示例5: lessFunc
// lessFunc returns a function that implements the "<" operator
// for the given type, or nil if the type doesn't support "<" .
func lessFunc(t reflect.Type) func(v1, v2 interface{}) bool {
switch t.Kind() {
case reflect.String:
return func(v1, v2 interface{}) bool { return v1.(string) < v2.(string) }
case reflect.Int:
return func(v1, v2 interface{}) bool { return v1.(int) < v2.(int) }
case reflect.Int8:
return func(v1, v2 interface{}) bool { return v1.(int8) < v2.(int8) }
case reflect.Int16:
return func(v1, v2 interface{}) bool { return v1.(int16) < v2.(int16) }
case reflect.Int32:
return func(v1, v2 interface{}) bool { return v1.(int32) < v2.(int32) }
case reflect.Int64:
return func(v1, v2 interface{}) bool { return v1.(int64) < v2.(int64) }
case reflect.Uint:
return func(v1, v2 interface{}) bool { return v1.(uint) < v2.(uint) }
case reflect.Uint8:
return func(v1, v2 interface{}) bool { return v1.(uint8) < v2.(uint8) }
case reflect.Uint16:
return func(v1, v2 interface{}) bool { return v1.(uint16) < v2.(uint16) }
case reflect.Uint32:
return func(v1, v2 interface{}) bool { return v1.(uint32) < v2.(uint32) }
case reflect.Uint64:
return func(v1, v2 interface{}) bool { return v1.(uint64) < v2.(uint64) }
case reflect.Float32:
return func(v1, v2 interface{}) bool { return v1.(float32) < v2.(float32) }
case reflect.Float64:
return func(v1, v2 interface{}) bool { return v1.(float64) < v2.(float64) }
default:
return nil
}
}
示例6: labelType
func labelType(t reflect.Type) bool {
switch t.Kind() {
case reflect.Interface, reflect.Struct:
return true
}
return false
}
示例7: checkCompositeLitR
// Recursively check composite literals, where a child composite lit's type depends the
// parent's type For example, the expression [][]int{{1,2},{3,4}} contains two
// slice lits, {1,2} and {3,4}, but their types are inferenced from the parent [][]int{}.
func checkCompositeLitR(lit *ast.CompositeLit, t reflect.Type, env Env) (*CompositeLit, []error) {
alit := &CompositeLit{CompositeLit: lit}
// We won't generate any errors here if the given type does not match lit.Type.
// The caller will need to detect the type incompatibility.
if lit.Type != nil {
var errs []error
lit.Type, t, _, errs = checkType(lit.Type, env)
if errs != nil {
return alit, errs
}
} else if t == nil {
return alit, []error{ErrMissingCompositeLitType{alit}}
}
alit.knownType = knownType{t}
if alit.CompositeLit.Elts != nil {
alit.Elts = make([]Expr, len(alit.CompositeLit.Elts))
}
switch t.Kind() {
case reflect.Map:
return checkCompositeLitMap(alit, t, env)
case reflect.Array, reflect.Slice:
return checkCompositeLitArrayOrSlice(alit, t, env)
case reflect.Struct:
return checkCompositeLitStruct(alit, t, env)
default:
panic("eval: unimplemented composite lit " + t.Kind().String())
}
}
示例8: readStructColumns
func readStructColumns(t reflect.Type) (cols []*ColumnMap, version *ColumnMap) {
n := t.NumField()
for i := 0; i < n; i++ {
f := t.Field(i)
if f.Anonymous && f.Type.Kind() == reflect.Struct {
// Recursively add nested fields in embedded structs.
subcols, subversion := readStructColumns(f.Type)
cols = append(cols, subcols...)
if subversion != nil {
version = subversion
}
} else {
columnName := f.Tag.Get("db")
if columnName == "" {
columnName = f.Name
}
cm := &ColumnMap{
ColumnName: columnName,
Transient: columnName == "-",
fieldName: f.Name,
gotype: f.Type,
}
cols = append(cols, cm)
if cm.fieldName == "Version" {
version = cm
}
}
}
return
}
示例9: marshalSimple
func (p *printer) marshalSimple(typ reflect.Type, val reflect.Value) (string, []byte, error) {
switch val.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return strconv.FormatInt(val.Int(), 10), nil, nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return strconv.FormatUint(val.Uint(), 10), nil, nil
case reflect.Float32, reflect.Float64:
return strconv.FormatFloat(val.Float(), 'g', -1, val.Type().Bits()), nil, nil
case reflect.String:
return val.String(), nil, nil
case reflect.Bool:
return strconv.FormatBool(val.Bool()), nil, nil
case reflect.Array:
if typ.Elem().Kind() != reflect.Uint8 {
break
}
// [...]byte
var bytes []byte
if val.CanAddr() {
bytes = val.Slice(0, val.Len()).Bytes()
} else {
bytes = make([]byte, val.Len())
reflect.Copy(reflect.ValueOf(bytes), val)
}
return "", bytes, nil
case reflect.Slice:
if typ.Elem().Kind() != reflect.Uint8 {
break
}
// []byte
return "", val.Bytes(), nil
}
return "", nil, &UnsupportedTypeError{typ}
}
示例10: getFields
func getFields(typ reflect.Type) *fields {
numField := typ.NumField()
fs := newFields(numField)
for i := 0; i < numField; i++ {
f := typ.Field(i)
if f.PkgPath != "" && !f.Anonymous {
continue
}
name, opts := parseTag(f.Tag.Get("msgpack"))
if name == "-" {
continue
}
if opts.Contains("inline") {
inlineFields(fs, f)
continue
}
if name == "" {
name = f.Name
}
field := field{
name: name,
index: f.Index,
omitEmpty: opts.Contains("omitempty"),
encoder: getEncoder(f.Type),
decoder: getDecoder(f.Type),
}
fs.Add(&field)
}
return fs
}
示例11: fixupArgs
func fixupArgs(t reflect.Type, v ...interface{}) []reflect.Value {
res := make([]reflect.Value, len(v))
for ix, vv := range v {
res[ix] = fixupArg(t.In(ix), vv)
}
return res
}
示例12: sliceElementType
// Returns the type of the elements of N slice(s). If the type is different,
// another slice or undefined, returns an error.
func sliceElementType(slices ...[]interface{}) (reflect.Type, error) {
var prevType reflect.Type
for _, s := range slices {
// Go through elements of all given slices and make sure they are all the same type.
for _, v := range s {
currentType := reflect.TypeOf(v)
if prevType == nil {
prevType = currentType
// We don't support lists of lists yet.
if prevType.Kind() == reflect.Slice {
return nil, errNoListOfLists
}
} else {
if prevType != currentType {
return nil, fmt.Errorf("list element types are not identical: %v", fmt.Sprint(slices))
}
prevType = currentType
}
}
}
if prevType == nil {
return nil, fmt.Errorf("no elements in any of the given slices")
}
return prevType, nil
}
示例13: getParams
func getParams(elem reflect.Type) []Parameter {
parameters := []Parameter{}
l := elem.NumField()
for i := 0; i < l; i++ {
f := elem.Field(i)
jsonName := f.Tag.Get("json")
if f.Anonymous {
parameters = append(parameters, getParams(f.Type)...)
} else if jsonName != "" {
p := Parameter{}
p.Name = jsonName
p.Type = f.Type.String()
p.Description = f.Tag.Get("description")
enum := f.Tag.Get("enum")
if enum != "" {
p.EnumValues = strings.Split(enum, ",")
p.Type = "enum"
} else {
p.EnumValues = []string{}
}
parameters = append(parameters, p)
}
}
return parameters
}
示例14: fixArgs
func fixArgs(args []reflect.Value, lastParamType reflect.Type, context Context) []reflect.Value {
if lastParamType.String() == "interface {}" ||
lastParamType.String() == "hprose.Context" {
args = append(args, reflect.ValueOf(context))
}
return args
}
示例15: convert
// Convert a string into the specified type. Return the type's zero value
// if we receive an empty string
func convert(t reflect.Type, value string) (reflect.Value, error) {
if value == "" {
return reflect.ValueOf(nil), nil
}
var d time.Duration
switch t {
case reflect.TypeOf(d):
result, err := time.ParseDuration(value)
return reflect.ValueOf(result), err
default:
}
switch t.Kind() {
case reflect.String:
return reflect.ValueOf(value), nil
case reflect.Int:
return parseInt(value)
case reflect.Bool:
return parseBool(value)
}
return reflect.ValueOf(nil), conversionError(value, `unsupported `+t.Kind().String())
}