我正在开发一个项目,使用来自Kafka的消息,使用消息包或json格式取消命令,构建它的sql并将它们插入到TDengine数据库中。
接口定义为:
type TaosEncoder interface {
TaosDatabase() string
TaosSTable() string
TaosTable() string
TaosTags() []interface{}
TaosCols() []string
TaosValues() []interface{}
}
对于名为 :Record
type Record struct {
DeviceID string `json:"deviceID"` // for table name
Timestamp time.Time `json:"timestamp"`
Cols []interface{} `json:"cols"`
}
实现接口:
func (r *Record) TaosCols() []string {
var tags []string
return tags
}
// other methods are implemented too
方法将使用接口方法:
func ToTaosBatchInsertSql(l []interface{}) (string, error) {
records := make([]string, len(l))
for i, t := range l {
// type TaosEncoderT = *TaosEncoder
record, err := ToTaosRecordSql(t.(TaosEncoder))
if err != nil {
return "", err
}
records[i] = record
}
return fmt.Sprintf("insert into %s", strings.Join(records, " ")), nil
}
将其用作:
records := make([]interface{}, size)
for i := 0; i < size; i++ {
item := <-q.queue # an channel of Record, defined as: queue chan interface{}
records[i] = item
}
sqlStr, err := utils.ToTaosBatchInsertSql(records)
它编译正常,但在像这样运行时失败:
panic: interface conversion: record.Record is not utils.TaosEncoder: missing method TaosCols
goroutine 71 [running]:
xxx/pkg/utils.ToTaosBatchInsertSql(0xc000130c80, 0xc8, 0xc8, 0xc8, 0x0, 0x0, 0x0)
但是当我更改记录的实现时 - 删除*
func (r Record) TaosCols() []string {
var tags []string
return tags
}
然后它就起作用了。
我已经告诉在接口实现中使用,所以问题是:*
可以实现与 的接口吗?func (r Record) xxx
如果没有,我该怎么办?
手掌心
湖上湖
相关分类