-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Closed
Description
my struct:
package model
import (
jsoniter "github.com/json-iterator/go"
"gorm.io/gorm"
)
type LoginUserInfo struct {
UserId int64
Username string
}
func (s *LoginUserInfo) MarshalBinary() (data []byte, err error) {
return jsoniter.Marshal(s)
}
func (s *LoginUserInfo) UnmarshalBinary(data []byte) error {
return jsoniter.Unmarshal(data, &s)
}
use:
future := time.Duration(3600) * time.Second
mu := model.LoginUserInfo{
UserId: row.UserId,
Username: row.Username,
}
if cmd = redisClient.Set(c, my_consts.LoginUserKey+tokenKey, mu, future); cmd.Err() != nil {
fmt.Println("保存登录信息失败:" + cmd.Err().Error())
return
}
fmt.Println("user has been saved to redis")
Expected Behavior
user has been saved to redis
Current Behavior
保存登录信息失败:redis: can't marshal model.LoginUserInfo (implement encoding.BinaryMarshaler)
Possible Solution
change the model declaration 【attation: receiver is changed to normal instance, not pointer instance】:
package model
import (
jsoniter "github.com/json-iterator/go"
"gorm.io/gorm"
)
type LoginUserInfo struct {
UserId int64
Username string
}
func (s LoginUserInfo) MarshalBinary() (data []byte, err error) {
return jsoniter.Marshal(s)
}
func (s *LoginUserInfo) UnmarshalBinary(data []byte) error {
return jsoniter.Unmarshal(data, s)
}
NOW, user has been saved to redis.
Steps to Reproduce
- my struct receiver is generic struct instance, it's can be marshal and save into redis
- my struct receiver is pointer reference struct instance, it's cannot be marshal and save into redis
Context (Environment)
github.com/redis/go-redis/v9 v9.0.2
Detailed Description
when my struct as normal instance (not pointer receiver) implement encoding.BinaryMarshaler, I can marshaler the struct instance into redis.
when my struct as instance (is a pointer receiver) implement encoding.BinaryMarshaler, I can't marshaler the struct instance into redis.
WHY???
Metadata
Metadata
Assignees
Labels
No labels