Go语言有哪些简便的方法可以将数据转换为字节或字符串?

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

What ways does Go have to easily convert data into bytes or strings

问题

我一直在使用Google App Engine Go SDK开发一些应用程序,这些应用程序使用Memcache作为从Datastore加载数据的缓冲区。由于Memcache只能将数据存储为[]byte,所以我经常需要创建函数将各种结构编码为字符串,以及反向的函数。不用说,当我需要做这种事情5次以上时,这是相当繁琐的。

是否有一种简单的方法可以将可以存储在Datastore中的任意结构转换为[]byte,以便将其存储在Memcache中,然后加载回来,而无需为GAE Golang中的各种结构创建自定义代码?

英文:

I've been developing a couple apps using Google App Engine Go SDK that use Memcache as a buffer for loading data from the Datastore. As the Memcache is only able to store data as []byte, I often find myself creating functions to encode the various structures as strings, and also functions to reverse the process. Needless to say, it is quite tedious when I need to do this sort of thing 5 times over.

Is there an easy way to convert any arbitrary structure that can be stored in Datastore into []byte in order to store it in Memcache and then load it back without having to create custom code for various structures in GAE Golang?

答案1

得分: 6

http://golang.org/pkg/encoding/gob或http://golang.org/pkg/encoding/json可以将任意数据类型转换为[]byte切片,只要编码的数据结构符合某些规则。如果您需要与其他语言共享数据,您可能需要使用json,因为它更容易共享。但是,gob会将数据编码为更小的大小。

英文:

http://golang.org/pkg/encoding/gob or http://golang.org/pkg/encoding/json can turn arbitrary datatypes into []byte slices given certain rules apply to the datastructures being encoded. You probably want one of them gob will encode to smaller sizes but json is more easily shareable with other languages if that is a requirement.

答案2

得分: 0

我发现自己也需要同样的东西。所以我创建了一个叫做:

AEGo/ds

文档 | 源码

go get github.com/scotch/aego/ds

它使用与"appengine/datastore"相同的API,因此它可以作为一个替代品使用。

import "github.com/scotch/aego/v1/ds"

u = &User{Name: "Bob"}
key := datastore.NewKey(c, "User", "bob", 0, nil)
key, err := ds.Put(c, key, u)

u = new(User)
err = ds.Get(c, key, u)

默认情况下,它会将所有的PutGet缓存到memcache中,但是您可以通过调用ds.Register方法来修改这个行为:

ds.Register("User", true, false, false)

Register方法接受一个表示Kind的字符串和3个bool值 - userDatastoreuseMemcacheuseMemory。传递一个true值将导致AEgo/ds将记录持久化到该存储中。Memory存储对于您不希望更改的记录非常有用,但如果您有多个实例运行,则可能包含过期数据。

支持的方法有:

Put
PutMulti
Get
GetMulti
Delete
DeleteMulti
AllocateIDs

注意:目前只有Get会进行缓存。GetMulti从数据存储中获取。

AEGo/ds是一个正在进行中的工作,但代码经过了充分测试。欢迎任何反馈。

为了回答你的问题,这是我如何将实体序列化为gob以进行memcache持久化。

英文:

I found myself needing the same thing. So I created a package called:

AEGo/ds

Documentation | Source

go get github.com/scotch/aego/ds

It uses the same API as the "appengine/datastore" so It will work as a drop in replacement.

import "github.com/scotch/aego/v1/ds"

u = &User{Name: "Bob"}
key := datastore.NewKey(c, "User", "bob", 0, nil)
key, err := ds.Put(c, key, u)

u = new(User)
err = ds.Get(c, key, u)

By default it will cache all Puts and Gets to memcache, but you can modify this behavior by calling the ds.Register method:

ds.Register("User", true, false, false)

The Register method takes a string representing the Kind and 3 bool - userDatastore, useMemcache, useMemory. Passing a true value will cause AEgo/ds to persist the record to that store. The Memory store is useful for records that you do not expect to change, but could contain stale data if you have more then one instance running.

Supported methods are:

Put
PutMulti
Get
GetMulti
Delete
DeleteMulti
AllocateIDs

Note: Currently cashing only occures with Get. GetMulti pulls from the datastore.

AEGo/ds is a work in progress, but the code is well tested. Any feedback would be appreciated.

And to answer you question here's how I serialized the entities to gob for the memcache persistence.

huangapple
  • 本文由 发表于 2012年7月11日 03:12:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/11420275.html
匿名

发表评论

匿名网友

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

确定