I like Redis streams because Redis becomes aka IoT database with it. Main Redis command for adding something to stream is XADD:
XADD log * name Some_name_to_write text Some_text_to_write
It works fine.
Then I try to use Redigo to do the same operation:
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
here is redis.Conn
.
This code produces the following output:
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
So, I see that arguments for conn.Do
exactly the same I use before in CLI.
What's wrong here?
Problem lies here
args = make([]string, 6)
You are creating a slice of string with length 6 but when you call append on it, it adds new item to the slice. Meaning your slice is now has length of 12 not 6. (It has 6 empty strings at the beginning instead of redis commands).
So either just call make with 0 (or just delete the line it will do the same since variable has been declared beforehand)
args = make([]string, 0)