doushu7588 2017-09-30 16:14
浏览 101
已采纳

Golang抽象函数从db获取数据并填充数组

I want to create an abstract function, that gets data from DB and fills array by this data. Types of array can be different. And I want to do it without reflect, due to performance issues. I just want to call everywhere some function like GetDBItems() and get array of data from DB with desired type. But all implementations that I create are owful.

Here is this function implementation:

type AbstractArrayGetter func(size int) []interface{}

func GetItems(arrayGetter AbstractArrayGetter) {
    res := DBResponse{}
    DB.Get(&res)
    arr := arrayGetter(len(res.Rows))
    for i := 0; i < len(res.Rows); i++ {
        json.Unmarshal(res.Rows[i].Value, &obj[i])
    }
}

Here I call this function:

var events []Event
GetFullItems("events", "events_list", map[string]interface{}{}, func(size int) []interface{} {
        events = make([]Event, size, size)
        proxyEnt := make([]interface{}, size, size)
        for i, _ := range events {
            proxyEnt[i] = &events[i]
        }
        return proxyEnt
    })

It works, but there are to much code to call this function, also there is some perfomance issue about copying events array to interfaces array.

How can I do it without reflect and do it with a short function call code? Or reflect not to slow in this case?

  • 写回答

1条回答 默认 最新

  • drkjzk3359 2017-10-05 17:13
    关注

    I tested performance with reflect, and it is similar to the mentioned above solution. So here is solution with reflect, if someone needs it. This function gets data from DB and fills abstract array

    func GetItems(design string, viewName string, opts map[string]interface{}, arrayType interface{}) (interface{}, error) {
        res := couchResponse{}
        opts["limit"] = 100000
        bytes, err := CouchView(design, viewName, opts)
        if err != nil {
            return nil, err
        }
        err = json.Unmarshal(bytes, &res)
        if err != nil {
            return nil, err
        }
        dataType := reflect.TypeOf(arrayType)
        slice := reflect.MakeSlice(reflect.SliceOf(dataType), len(res.Rows), len(res.Rows))
        for i := 0; i < len(res.Rows); i++ {
            if opts["include_docs"] == true {
                err = json.Unmarshal(res.Rows[i].Doc, slice.Index(i).Addr().Interface())
            } else {
                err = json.Unmarshal(res.Rows[i].Value, slice.Index(i).Addr().Interface())
            }
            if err != nil {
                return nil, err
            }
        }
        x := reflect.New(slice.Type())
        x.Elem().Set(slice)
        return x.Interface(), nil
    }
    

    and getting data using this function:

    var e Event
    res, err := GetItems("data", "data_list", map[string]interface{}{}, e)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格