redis: 无法序列化 map[string]string(实现 encoding.BinaryMarshaler)—-在 Redis 接口中

huangapple go评论133阅读模式
英文:

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: can't marshal map[string]string (implement encoding.BinaryMarshaler)"。
我尝试使用marshal函数,但没有用。
我希望你能帮助我解决这个问题。

英文:

I want to create a generic Redis interface for storing and getting values.
I am beginner to Golang and redis.
If there are any changes to be done to the code I would request you to help me.

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
}

When i run this code i receive an error of "redis: can't marshal map[string]string (implement encoding.BinaryMarshaler)".
I have tried using marshal but there was no use.
I would request you to help me with this.

答案1

得分: 5

go的非标量类型不能直接转换为redis的存储结构,因此在存储之前需要进行转换。

如果你想实现一个通用的方法,那么该方法应该接收一个可以直接存储的类型,调用者负责将复杂的结构转换为可用的类型,例如:

// ...

    student := map[string]string{
        "id":   "st01",
        "name": "namme1",
    }

    // 这里应该处理错误
    bs, _ := json.Marshal(student)

    set("key1", bs, 0)

// ...

func set(key string, value interface{}, ttl int) bool {
    // ...
}

一个具体的方法可以针对特定的结构进行结构化,但是该结构应该实现序列化器 encoding.MarshalBinaryencoding.UnmarshalBinary,例如:

type Student map[string]string

func (s Student) MarshalBinary() ([]byte, error) {
    return json.Marshal(s)
}

// 确保这里的Student接口接受一个指针
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 {
    // ...
}
英文:

The non-scalar type of go cannot be directly converted to the storage structure of redis, so the structure needs to be converted before storage

If you want to implement a general method, then the method should receive a type that can be stored directly, and the caller is responsible for converting the complex structure into a usable type, for example:

// ...

    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 {
    // ...
}

A specific method can structure a specific structure, but the structure should implement the serializers encoding.MarshalBinary and encoding.UnmarshalBinary, for example:

type Student map[string]string

func (s Student) MarshalBinary() ([]byte, error) {
    return json.Marshal(s)
}

// make sure the Student interface here accepts a pointer
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 {
    // ...
}

huangapple
  • 本文由 发表于 2021年11月16日 23:19:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/69991754.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定