Golang 反射改进

huangapple go评论87阅读模式
英文:

Golang Reflection Improvement

问题

有没有人知道更好的方法来做这个?

目标是将自定义定义的字段从字符串转换回其整数类型。

switch val.Kind() {
    case reflect.Int:
        intID, err := strconv.ParseInt(id, 10, 0)
        if err != nil {
            return err
        }
        val.Set(reflect.ValueOf(int(intID)))

    case reflect.Int8:
        intID, err := strconv.ParseInt(id, 10, 8)
        if err != nil {
            return err
        }
        val.Set(reflect.ValueOf(int8(intID)))

    case reflect.Int16:
        intID, err := strconv.ParseInt(id, 10, 16)
        if err != nil {
            return err
        }
        val.Set(reflect.ValueOf(int16(intID)))

    case reflect.Int32:
        intID, err := strconv.ParseInt(id, 10, 32)
        if err != nil {
            return err
        }
        val.Set(reflect.ValueOf(int32(intID)))

    case reflect.Int64:
        intID, err := strconv.ParseInt(id, 10, 64)
        if err != nil {
            return err
        }
        val.Set(reflect.ValueOf(intID))
}

有没有更好的方法来实现这个功能?

英文:

Anyone know a better way to do this?
The goal is for a custom defined Field to be casted back again from a string to its int type.

switch val.Kind() {
    case reflect.Int:
            intID, err := strconv.ParseInt(id, 10, 0)
            if err != nil {
                    return err
            }
            val.Set(reflect.ValueOf(int(intID)))

    case reflect.Int8:
            intID, err := strconv.ParseInt(id, 10, 8)
            if err != nil {
                    return err
            }
            val.Set(reflect.ValueOf(int8(intID)))

    case reflect.Int16:
            intID, err := strconv.ParseInt(id, 10, 16)
            if err != nil {
                    return err
            }
            val.Set(reflect.ValueOf(int16(intID)))

    case reflect.Int32:
            intID, err := strconv.ParseInt(id, 10, 32)
            if err != nil {
                    return err
            }
            val.Set(reflect.ValueOf(int32(intID)))

    case reflect.Int64:
            intID, err := strconv.ParseInt(id, 10, 64)
            if err != nil {
                    return err
            }
            val.Set(reflect.ValueOf(intID))
   }

答案1

得分: 6

你可以使用Value.SetInt / Value.SetUint

func setId(id string, v interface{}) {
    // 错误检查留作练习
    val := reflect.ValueOf(v).Elem() // 如果v不是指针,这将引发恐慌
    switch val.Kind() {
    case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
        idn, _ := strconv.ParseInt(id, 10, 64)
        if val.OverflowInt(idn) {
            // 处理大值
        }
        val.SetInt(idn)
    case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
        idn, _ := strconv.ParseUint(id, 10, 64)
        if val.OverflowUint(idn) {
            // 处理大值
        }
        val.SetUint(idn)
    }
}

[kbd] [playground](http://play.golang.org/p/2gS4hQ-T-K) [/kbd]
英文:

You can use Value.SetInt / Value.SetUint:

func setId(id string, v interface{}) {
	// error checking is left as an exercise
	val := reflect.ValueOf(v).Elem() // this will panic if v isn't a pointer
	switch val.Kind() {
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		idn, _ := strconv.ParseInt(id, 10, 64)
		if val.OverflowInt(idn) {
			// handle large values
		}
		val.SetInt(idn)
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		idn, _ := strconv.ParseUint(id, 10, 64)
		if val.OverflowUint(idn) {
			// handle large values
		}
		val.SetUint(idn)
	}
}

<kbd>playground</kbd>

huangapple
  • 本文由 发表于 2014年10月9日 17:41:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/26275198.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定