redis.SAdd("words", "1213")
redis: can't marshal []interface {} (consider implementing encoding.BinaryMarshaler)
Your code should be more like redis.SAdd("words", []interface{}{"1213", "12123"}).
I want do save one object into redis, but also have this problem.
code:
func SetUser(user UserBean) error {
key := fmt.Sprintf("user:%s", user.Name)
_, err := Client().Set(key, user, time.Hour).Result()
return err
}
You can do two things, choose what works better for you:
encoding.BinaryMarshaler and encoding.BinaryUnMarshaler for your type and Redis will do it automatically all operations[]byte as json and store []byte in Redisencoding.BinaryMarshaler and encoding.BinaryUnMarshaler are interfaces methods that you would need to override in your particular struct.
type MyStruct struct{}
func(m *MyStruct)UnmarshalBinary(data []byte) error{
// convert data to yours, let's assume its json data
return json.Unmarshal(data, m)
}
The second case would be to store as []byte in Redis and manually do the same thing. Mind you I just typed by memory the code above, might have errors. Cheers.
Oh, I deleted my message to which you replied. I asked him for an example. Thank you a lot @mauleyzaola :)
i tried to use eval but still have the same error message after implement binary marshaller and unmarshaler. seems like the 1st solution is not working
that's worked
type MyStruct struct{}
func (m *MyStruct) MarshalBinary() ([]byte, error) {
return json.Marshal(m)
}
it passed when use MyStruct instead of *MyStruct.
Most helpful comment
You can do two things, choose what works better for you:
encoding.BinaryMarshalerandencoding.BinaryUnMarshalerfor your type and Redis will do it automatically all operations[]byteas json and store[]bytein Redis