我喜欢 Redis 流,因为它可以充当物联网数据库。向流中添加数据的主要 Redis 命令是 XADD:
XADD log * name Some_name_to_write text Some_text_to_write
它运行良好。
然后我尝试使用Redigo执行相同的操作:
type LogEntry struct {
Name string `json:"name"`
Text string `json:"text"`
}
func (l *LogEntry) Push() error {
var args []string // XADD arguments
log.Printf("Log Entry: %+v\n", l)
args = make([]string, 6)
args = append(args, "log") // Stream key
args = append(args, "*") // Auto-generated stream ID
args = append(args, "name", l.Name)
args = append(args, "text", l.Text)
log.Printf("Args for XADD: %+v\n", args)
new := make([]any, len(args))
for index, value := range args {
new[index] = value
}
_, err := connection.Do("XADD", new...)
if err != nil {
log.Println("XADD error", err)
return err
}
return nil
}
connection
这里是redis.Conn
。
此代码产生以下输出:
2025-04-08 16:03:08 2025/04/08 13:03:08 Log Entry: &{Name:Some_name_to_write Text:Some_text_to_write}
2025-04-08 16:03:08 2025/04/08 13:03:08 Args for XADD: [ log * name Some_name_to_write text Some_text_to_write]
2025-04-08 16:03:08 2025/04/08 13:03:08 XADD error ERR Invalid stream ID specified as stream command argument
因此,我看到的参数与conn.Do
我之前在 CLI 中使用的完全相同。
这里出了什么问题?