douzhang7603 2018-08-20 22:06
浏览 30

如何组合仅在参数类型上不同的两个函数

So I have this golang function that looks like this:

func addDaysListener(ch <-chan []entity.Day, db *sql.DB) {
    for days := range ch {
        if len(days) == 0 {
            continue
        }

        logger := logrus.WithFields(logrus.Fields{
            "ticker": days[0].Ticker(),
            "count":  len(days),
        })
        if err := update.InsertDays(db, days); err != nil {
            logger.Error(err)
        } else {
            logger.Info("Inserted Days")
        }
    }
}

I also have a function called addMinutesListener() that is identical EXCEPT:

  • It listens on a <-chan []entity.Minute
  • It calls update.InsertMinutes()

Both entity.Day and entity.Minute implement datum.Candle, although the update functions require the specific type. I'd like to write something like this:

func addItemsListener(db *sql.DB, ch <-chan []datum.Candle, insertFunc func(*sql.DB, []datum.Candle) error) {
    for items := range ch {
        if len(items) == 0 {
            continue
        }

        logger := logrus.WithFields(logrus.Fields{
            "ticker": items[0].Ticker(),
            "count":  len(items),
        })
        if err := insertFunc(db, items); err != nil {
            logger.Error(err)
        } else {
            logger.Info("Inserted Days")
        }
    }
}

...Except the function signature for update.InsertDays and update.InsertMinutes both require the specific type, not the generic type, and the channels are set up like that as well. I could potentially change the channels, but I really can't change the insert functions because they do require the correct type as they insert the data into the database and the fields (properties) vary greatly between the two.

I feel like the correct answer to this problem may be something far more sinister, which is why I'm asking you, SO!

I'm sorry for the nondescript title; please feel free to comment if you have better ideas.

  • 写回答

1条回答 默认 最新

  • dongyi2425 2018-08-20 22:54
    关注

    use type assertions

    func addDaysListener(ch <-chan []datum.Candle , db *sql.DB) {
        for days := range ch {
            if len(days) == 0 {
                continue
            }
    
            logger := logrus.WithFields(logrus.Fields{
               "ticker": days[0].Ticker(),
               "count":  len(days),
           })
    
         switch v:=days.(type){
           case  []entity.Day:
                if err := update.InsertDays(db,v); err != nil {
                   logger.Error(err)
                } else {
                   logger.Info("Inserted Days")
                }
           case  []entity.Minute:
                if err := update.InsertMinutes(db, v); err != nil {
                   logger.Error(err)
                } else {
                   logger.Info("Inserted Minutes")
                }
    
          }
    
       }
      }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据