-
Hi! According to the documentation, I should be able to perform an HSet Command using an struct. In the documentation I found this example:
When when trying to run the example type Info struct {
Testgroup int `json:"testgroup" redis:"testgroup"`
Status string `json:"status" redis:"status"`
}
func main() {
rdb := redis.NewClient(&redis.Options{
Addr: ":6379",
})
ctx := context.Background()
cmd := rdb.HSet(context.Background(), "mysupermap", models.Info{6, "RUNNING"})
i, err := cmd.Result()
fmt.Println(i, err)
} I am getting this error: ERR wrong number of arguments for 'hset' command What am I doing wrong? Thank you very much!! |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 6 replies
-
I ran your example and it worked fine, the version I'm using is redis/go-redis/v9@v9.0.2 |
Beta Was this translation helpful? Give feedback.
-
my go version: go1.20, redis-version: 7.0.8, go-redis version: v9.0.2 type Per struct {
Name string `redis:"name"`
Age int `redis:"age"`
}
func main() {
client := redis.NewClient(&redis.Options{
Addr: "127.0.0.1:6379",
})
ctx := context.Background()
err := client.HSet(ctx, "myhash", Per{Name: "hi", Age: 20}).Err()
fmt.Println(err) // print: nil
}
127.0.0.1:6379> hgetall myhash
1) "name"
2) "hi"
3) "age"
4) "20"
127.0.0.1:6379> |
Beta Was this translation helpful? Give feedback.
-
Note that in older versions of Redis server(redis-server < 4.0), HSet only supports a single key-value pair. redis-docs: https://redis.io/commands/hset (Starting with Redis version 4.0.0: Accepts multiple field and value arguments.) If you are using a Struct type and the number of fields is greater than one, you will receive an error similar to redis-server >= 4.0(HSet): client.HSet(ctx, "myhash", Per{Name: "hi", Age: 20}) redis-server < 4.0(HMSet): client.HMSet(ctx, "myhash", Per{Name: "hi", Age: 20}) Please note that in Redis, HMSet has been marked as |
Beta Was this translation helpful? Give feedback.
-
hello! @monkey92t. client.HSet(context.Background(), "myhash", Per{Name: "hi", Age: 20})
|
Beta Was this translation helpful? Give feedback.
-
You seem to be using a lower version of go-redis. Could you please provide your redis-server version and go-redis version? |
Beta Was this translation helpful? Give feedback.
-
Note that I also get the client.HSet(ctx, "myhash", map[string]interface{}{"name": "hi", "age": 20}) However, I don't experience it when my map is only composed by one field. The issue has been found on version 8.11.5. |
Beta Was this translation helpful? Give feedback.
Note that in older versions of Redis server(redis-server < 4.0), HSet only supports a single key-value pair.
redis-docs: https://redis.io/commands/hset (Starting with Redis version 4.0.0: Accepts multiple field and value arguments.)
If you are using a Struct type and the number of fields is greater than one, you will receive an error similar to
ERR wrong number of arguments
, you can use HMSet as a substitute.redis-server >= 4.0(HSet):
redis-server < 4.0(HMSet):
Please note that in Redis, HMSet has been marked as
deprecated
, and it may not be supported in the future.#2503