当前位置: 首页>>代码示例>>Golang>>正文


Golang Kind.String方法代码示例

本文整理汇总了Golang中reflect.Kind.String方法的典型用法代码示例。如果您正苦于以下问题:Golang Kind.String方法的具体用法?Golang Kind.String怎么用?Golang Kind.String使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在reflect.Kind的用法示例。


在下文中一共展示了Kind.String方法的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.String方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。