當前位置: 首頁>>代碼示例>>Golang>>正文


Golang reflect.Kind類代碼示例

本文整理匯總了Golang中reflect.Kind的典型用法代碼示例。如果您正苦於以下問題:Golang Kind類的具體用法?Golang Kind怎麽用?Golang Kind使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Kind類的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: Type

// Type sets the expected reflect.Kind type an option will accept.
func (f *Option) Type(kind reflect.Kind) *Option {
	invalidKinds := []reflect.Kind{
		reflect.Uintptr,
		reflect.Complex64,
		reflect.Complex128,
		reflect.Array,
		reflect.Chan,
		reflect.Func,
		reflect.Interface,
		reflect.Map,
		reflect.Ptr,
		reflect.Slice,
		reflect.Struct,
		reflect.UnsafePointer,
	}

	for _, bad := range invalidKinds {
		if kind == bad {
			panic(fmt.Sprintf("Cannot use kind: '%s' as a valid type", kind.String()))
		}
	}

	f.ExpectedType = kind
	return f
}
開發者ID:clagraff,項目名稱:argparse,代碼行數:26,代碼來源:option.go

示例2: KindOf

// KindOf asserts that the matched value of the kind.
func (m *Matcher) KindOf(kind reflect.Kind) *Matcher {
	rv := reflect.ValueOf(m.value)
	if rv.Kind() != kind {
		m.t.Errorf("expected kind %s, got %s", kind.String(), rv.Kind().String())
	}
	return m
}
開發者ID:zhevron,項目名稱:match,代碼行數:8,代碼來源:matcher.go

示例3: cannotConvert

func cannotConvert(L *lua.State, idx int, msg string, kind reflect.Kind, t reflect.Type) {
	types := ""
	if t != nil {
		types = t.String()
	} else {
		types = kind.String()
	}
	RaiseError(L, sprintf("arg #%d cannot convert %s to %s", idx, msg, types))
}
開發者ID:imvu,項目名稱:Tetra,代碼行數:9,代碼來源:luar.go

示例4: AddSimpleArrayField

func (this *typeScriptClassBuilder) AddSimpleArrayField(fieldName, fieldType string, kind reflect.Kind) error {
	if typeScriptType, ok := this.types[kind]; ok {
		if len(fieldName) > 0 {
			this.fields += fmt.Sprintf("%s%s: %s[];\n", this.indent, fieldName, typeScriptType)
			this.createFromMethodBody += fmt.Sprintf("%s%sresult.%s = source[\"%s\"];\n", this.indent, this.indent, fieldName, fieldName)
			return nil
		}
	}
	return errors.New(fmt.Sprintf("Cannot find type for %s (%s/%s)", kind.String(), fieldName, fieldType))
}
開發者ID:tkrajina,項目名稱:typescriptify-golang-structs,代碼行數:10,代碼來源:typescriptify.go

示例5: Atok

// convert string to reflect.Value acoring to kind( use base 0 to parse numeric )
func Atok(str string, k reflect.Kind) (output reflect.Value, err error) {
	switch k {
	case reflect.Bool:
		b, e := strconv.ParseBool(str)
		return reflect.ValueOf(b), e
	case reflect.Int:
		i, e := strconv.Atoi(str)
		return reflect.ValueOf(i), e
	case reflect.Int8:
		i, e := strconv.ParseInt(str, 0, 8)
		return reflect.ValueOf(int8(i)), e
	case reflect.Int16:
		i, e := strconv.ParseInt(str, 0, 16)
		return reflect.ValueOf(int16(i)), e
	case reflect.Int32:
		i, e := strconv.ParseInt(str, 0, 32)
		return reflect.ValueOf(int32(i)), e
	case reflect.Int64:
		i, e := strconv.ParseInt(str, 0, 64)
		return reflect.ValueOf(i), e
	case reflect.Uint:
		i, e := strconv.ParseUint(str, 0, 0)
		return reflect.ValueOf(uint(i)), e
	case reflect.Uint8:
		i, err := strconv.ParseUint(str, 0, 8)
		return reflect.ValueOf(uint8(i)), err
	case reflect.Uint16:
		i, err := strconv.ParseUint(str, 0, 16)
		return reflect.ValueOf(uint16(i)), err
	case reflect.Uint32:
		i, err := strconv.ParseUint(str, 0, 32)
		return reflect.ValueOf(uint32(i)), err
	case reflect.Uint64:
		i, err := strconv.ParseUint(str, 0, 64)
		return reflect.ValueOf(i), err
	case reflect.Float32:
		i, err := strconv.ParseFloat(str, 32)
		return reflect.ValueOf(float32(i)), err
	case reflect.Float64:
		i, err := strconv.ParseFloat(str, 64)
		return reflect.ValueOf(i), err
	case reflect.String:
		return reflect.ValueOf(str), nil
	default:
	}
	err = newTypeErr(methodName(), "can not convert string to "+k.String(), nil)
	return
}
開發者ID:sdming,項目名稱:kiss,代碼行數:49,代碼來源:convert.go

示例6: getSetting

func getSetting(config map[string]interface{}, setting string, kind reflect.Kind,
	mandatory bool, fallbackValue interface{}) (interface{}, error) {
	if _, ok := config[setting]; !ok {
		if mandatory {
			return fallbackValue, fmt.Errorf("missing `%s' mandatory setting", setting)
		}

		return fallbackValue, nil
	}

	if reflect.ValueOf(config[setting]).Kind() != kind {
		return fallbackValue, fmt.Errorf("setting `%s' value should be a %s", setting, kind.String())
	}

	return config[setting], nil
}
開發者ID:krishkm-gm,項目名稱:facette,代碼行數:16,代碼來源:config.go

示例7: setPrim

func (t *Type) setPrim(k reflect.Kind) {
	t.Kind = k
	if t.Name == "" {
		t.Name = k.String()
	}
}
開發者ID:bobappleyard,項目名稱:go-pkg,代碼行數:6,代碼來源:pkg.go

示例8: parseAs

func parseAs(str string, kind reflect.Kind, dst reflect.Value) error {
	// Handle strings
	if kind == reflect.String {
		dst.SetString(str)
		return nil
	}

	// Handle booleans
	if kind == reflect.Bool {
		if strings.ToLower(str) == "true" ||
			strings.ToLower(str) == "yes" ||
			str == "1" {
			dst.SetBool(true)
			return nil
		}
		if strings.ToLower(str) == "false" ||
			strings.ToLower(str) == "no" ||
			str == "0" {
			dst.SetBool(false)
			return nil
		}
		return fmt.Errorf("String '%s' not recognized as a boolean value", str)
	}

	// Handle integers
	if kind >= reflect.Int && kind <= reflect.Int64 {
		size := 0
		if kind == reflect.Int8 {
			size = 8
		}
		if kind == reflect.Int16 {
			size = 16
		}
		if kind == reflect.Int32 {
			size = 32
		}
		if kind == reflect.Int64 {
			size = 64
		}
		i, err := strconv.ParseInt(str, 10, size)
		if err != nil {
			return fmt.Errorf("Error parsing '%s' as an integer: %v", str, err)
		}
		dst.SetInt(i)
		return nil
	}

	// Handle unsigned integers
	if kind >= reflect.Uint && kind <= reflect.Uint64 {
		size := 0
		if kind == reflect.Uint8 {
			size = 8
		}
		if kind == reflect.Uint16 {
			size = 16
		}
		if kind == reflect.Uint32 {
			size = 32
		}
		if kind == reflect.Uint64 {
			size = 64
		}
		i, err := strconv.ParseUint(str, 10, size)
		if err != nil {
			return fmt.Errorf("Error parsing '%s' as an unsigned integer: %v",
				str, err)
		}
		dst.SetUint(i)
		return nil
	}

	// Handle floats
	if kind >= reflect.Float32 || kind <= reflect.Float64 {
		size := 32
		if kind == reflect.Float64 {
			size = 64
		}
		i, err := strconv.ParseFloat(str, size)
		if err != nil {
			return fmt.Errorf("Error parsing '%s' as a float: %v", str, err)
		}
		dst.SetFloat(i)
		return nil
	}

	return fmt.Errorf("Unable to convert strings to %s", kind.String())
}
開發者ID:xogeny,項目名稱:unquery,代碼行數:87,代碼來源:unmarshal.go


注:本文中的reflect.Kind類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。