如何将此缓存项转换回映射切片?

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

How do I convert this cache item back to a slice of maps?

问题

我对Go还不太熟悉,正在尝试使用Beego的缓存。我可以将[]map[string]string放入缓存中,但是无法弄清楚如何将值转换回[]map[string]string。

例如,将项目放入缓存中:

m := make([]map[string]string)
// 向映射切片添加项目
.......
// 将其缓存起来
if err := c.Put("key", m, 100); err != nil {
    fmt.Println(err)
}
// 检索它
n := c.Get("key")
fmt.Println(reflect.TypeOf(n)) // ==> string

// 尝试失败
a := n.([]map[string]string)
fmt.Println(a) // 报错:接口转换:接口是字符串,而不是[]map[string]string

如何将n转换为映射切片?

英文:

I'm still new to Go and trying to use Beego's cache. I can put a []map[string]string into the cache but can't figure out how to convert the value back to a []map[string]string.

For instance, to put the item in the cache:

m:=make([]map[string]string)
// add items to the slice of maps
.......
// cache it
if err := c.Put("key", m, 100); err != nil {
    fmt.Println(err)
}
// retrieve it
n := c.Get("key")
fmt.Println(reflect.TypeOf(n)) // ==>string

// failed attempt
a := n.([]map[string]string)
fmt.Println(a) // panic: interface conversion: interface is string, not []map[string]string

How do I convert n to a slice of maps?

答案1

得分: 2

深入研究代码后发现,即使代码中使用了interface{},实际上它会将所有内容压缩成[]byte类型。

在执行Get操作时,它会将所有内容转换为string类型。

因此,你需要自己对数据结构进行编组/解组。

请参考以下示例,并将你的调用和错误检查替换为提供的示例代码:

package main

import (
	"bytes"
	"encoding/json"
	"log"
)

var cache map[string]string = make(map[string]string)

func Put(key, value string) {
	cache[key] = value
}

func Get(key string) string {
	return cache[key]
}

func main() {

	m := map[string]string{
		"A": "1",
		"B": "2",
	}
	if b, err := json.Marshal(m); err != nil {
		log.Fatal(err)
	} else {
		Put("myKey", string(b))
	}

	b := bytes.NewBufferString(Get("myKey"))

	var mm map[string]string
	if err := json.Unmarshal(b.Bytes(), &mm); err != nil {
		log.Fatal(err)
	}

	log.Printf("%#v", mm)
}

希望对你有所帮助!

英文:

well digging into the code seems like even if it says interface{} what it does it actually squash everything to []byte

https://github.com/astaxie/beego/blob/master/cache/memcache/memcache.go#L77

and when it Does the Get convert everything to string

https://github.com/astaxie/beego/blob/master/cache/memcache/memcache.go#L61

So you need to Marshal / Unmarshal the data structure yourself.

See this example and substitute your calls and error check with the dummies one provided:

http://play.golang.org/p/9z3KcOlgAx

package main

import (
	"bytes"
	"encoding/json"
	"log"
)

var cache map[string]string = make(map[string]string)

func Put(key, value string) {
	cache[key] = value
}

func Get(key string) string {
	return cache[key]
}

func main() {

	m := map[string]string{
		"A": "1",
		"B": "2",
	}
	if b, err := json.Marshal(m); err != nil {
		log.Fatal(err)
	} else {
		Put("myKey", string(b))
	}

	b := bytes.NewBufferString(Get("myKey"))

	var mm map[string]string
	if err := json.Unmarshal(b.Bytes(), &mm); err != nil {
		log.Fatal(err)
	}

	log.Printf("%#v", mm)
}

huangapple
  • 本文由 发表于 2014年12月3日 04:19:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/27258181.html
匿名

发表评论

匿名网友

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

确定