dongqing220586 2018-04-24 18:15
浏览 29

Golang断言任何数字浮动

I'm using a library that accepts only float64 as a function argument but my code can post any type of numbers in an interface{}, meaning int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64.

How can I write an elegant function that assert for all these types ?

I was thinking of something like:

if i, ok := event.Value.(int); ok {
 value = float64(i)
} else if f, ok := event.Value.(float64); ok {
 value = f
} else { 
  ... error and return
}

with one else if per type.

I'm sure there's something more efficient/elegant somewhere!

Thanks

  • 写回答

1条回答 默认 最新

  • dpsfay2510 2018-04-24 18:28
    关注

    The right way to do it would be like so: https://play.golang.org/p/_evcLsjcXY-

    var x interface{}
    x = int64(12)
    var y float64
    switch v := x.(type) {
    case int:
        y = float64(v)
    case int8:
        y = float64(v)
    case int16:
        y = float64(v)
    case int32:
        y = float64(v)
    case int64:
        y = float64(v)
    case uint:
        y = float64(v)
    case uint8:
        y = float64(v)
    case uint16:
        y = float64(v)
    case uint32:
        y = float64(v)
    case uint64:
        y = float64(v)
    case float32:
        y = float64(v)
    case float64:
        y = v
    }
    fmt.Println(y)
    

    If you don't care about performance / safety / etc., and just want something short (don't do this):

    var x interface{}
    x = int64(12)
    tmp, _ := json.Marshal(x)
    var y float64
    json.Unmarshal(tmp, &y)
    fmt.Println(y)
    

    Or, you could use reflection https://play.golang.org/p/QYQP00KicmF:

    import "reflect"
    
    var floatType = reflect.TypeOf(float64(0))
    
    func getFloat(unk interface{}) (float64, error) {
        v := reflect.ValueOf(unk)
        v = reflect.Indirect(v)
        if !v.Type().ConvertibleTo(floatType) {
            return 0, fmt.Errorf("cannot convert %v to float64", v.Type())
        }
        fv := v.Convert(floatType)
        return fv.Float(), nil
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分