I am having some data in redis. The value is stored in form of bytes. I want to fetch the bytes and map that to a struct but I am not able to decode bytes data. This is the code I am using to persist data to redis:
import (
bytes "bytes"
"encoding/gob"
"fmt"
"github.com/go-redis/redis"
"time"
)
var convertor bytes.Buffer
var encoder = gob.NewEncoder(&convertor)
var decoder = gob.NewDecoder(&convertor)
func Set(field string, value interface{}) (error){
id := "sample-key";
err := encoder.Encode(value)
if err!=nil {
fmt.Printf("error occured while marshaling data")
return err;
}
valueBytes := convertor.Bytes()
err = redisdb.HSet(id, field, valueBytes).Err();
if err!=nil{
fmt.Printf("error occured while setting data")
return err;
}
return nil;
}
This is how I get data from redis:
result, err := redisdb.HGet(key, field).Result()
if err != nil{
fmt.Println("error occured");
}
but I am not able to store it into struct. How can decode the bytes data to a struct?