redis: can't marshal map[string]string (implement encoding.BinaryMarshaler) ----在 Refis 接口
redis: can't marshal map[string]string (implement encoding.BinaryMarshaler) ----in Refis interface
我想创建一个通用的 Redis 接口来存储和获取值。
我是 Golang 和 Redis 的初学者。
如果需要对代码进行任何更改,我会请求您帮助我。
package main
import (
"fmt"
"github.com/go-redis/redis"
)
func main() {
student := map[string]string{
"id": "st01",
"name": "namme1",
}
set("key1", student, 0)
get("key1")
}
func set(key string, value map[string]string, ttl int) bool {
client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
})
err := client.Set(key, value, 0).Err()
if err != nil {
fmt.Println(err)
return false
}
return true
}
func get(key string) bool {
client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
})
val, err := client.Get(key).Result()
if err != nil {
fmt.Println(err)
return false
}
fmt.Println(val)
return true
}
当我 运行 此代码时,我收到错误消息“redis:无法编组映射 [string] 字符串(实现 encoding.BinaryMarshaler)”。
我试过使用元帅,但没有用。
我会请你帮我解决这个问题。
go的非标量类型不能直接转换成redis的存储结构,所以存储前需要转换结构
如果要实现一个通用的方法,那么该方法应该接收一个可以直接存储的类型,调用者负责将复杂的结构转换成可用的类型,例如:
// ...
student := map[string]string{
"id": "st01",
"name": "namme1",
}
// Errors should be handled here
bs, _ := json.Marshal(student)
set("key1", bs, 0)
// ...
func set(key string, value interface{}, ttl int) bool {
// ...
}
一个特定的方法可以构造一个特定的结构,但是该结构应该实现序列化器encoding.MarshalBinary
和encoding.UnmarshalBinary
,例如:
type Student map[string]string
func (s Student) MarshalBinary() ([]byte, error) {
return json.Marshal(s)
}
func (s Student) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, &s)
}
// ...
student := Student{
"id": "st01",
"name": "namme1",
}
set("key1", student, 0)
// ...
func set(key string, value Student, ttl int) bool {
// ...
}
我想创建一个通用的 Redis 接口来存储和获取值。 我是 Golang 和 Redis 的初学者。 如果需要对代码进行任何更改,我会请求您帮助我。
package main
import (
"fmt"
"github.com/go-redis/redis"
)
func main() {
student := map[string]string{
"id": "st01",
"name": "namme1",
}
set("key1", student, 0)
get("key1")
}
func set(key string, value map[string]string, ttl int) bool {
client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
})
err := client.Set(key, value, 0).Err()
if err != nil {
fmt.Println(err)
return false
}
return true
}
func get(key string) bool {
client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
})
val, err := client.Get(key).Result()
if err != nil {
fmt.Println(err)
return false
}
fmt.Println(val)
return true
}
当我 运行 此代码时,我收到错误消息“redis:无法编组映射 [string] 字符串(实现 encoding.BinaryMarshaler)”。 我试过使用元帅,但没有用。 我会请你帮我解决这个问题。
go的非标量类型不能直接转换成redis的存储结构,所以存储前需要转换结构
如果要实现一个通用的方法,那么该方法应该接收一个可以直接存储的类型,调用者负责将复杂的结构转换成可用的类型,例如:
// ...
student := map[string]string{
"id": "st01",
"name": "namme1",
}
// Errors should be handled here
bs, _ := json.Marshal(student)
set("key1", bs, 0)
// ...
func set(key string, value interface{}, ttl int) bool {
// ...
}
一个特定的方法可以构造一个特定的结构,但是该结构应该实现序列化器encoding.MarshalBinary
和encoding.UnmarshalBinary
,例如:
type Student map[string]string
func (s Student) MarshalBinary() ([]byte, error) {
return json.Marshal(s)
}
func (s Student) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, &s)
}
// ...
student := Student{
"id": "st01",
"name": "namme1",
}
set("key1", student, 0)
// ...
func set(key string, value Student, ttl int) bool {
// ...
}