Golang序列化和反序列化回来 [英] Golang serialize and deserialize back

查看:324
本文介绍了Golang序列化和反序列化回来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



例如,如果我有这个结构体,那么在Golang中将最好的方法(完整性和性能)序列化并反序列化为字符串,反之亦然?

  struct Session {
属性map [string] interface {}
权限[] int64
}

我想将它存储在 Redis 并将其取回。我试图保存,int和字符串,没问题,但如何存储结构对象?

  conn:= redisConnectors.Get ()

//示例

,err:= conn.Do(`SETEX`,`uid_key`,EXPIRE_SEC,user_id)
_,err = conn.Do(`SETEX`,`email_key`,EXPIRE_SEC,login_email)

//获取示例

user_id,err:= redis.Int64(conn.Do `GET`,`uid_key`))
login_email,err:= redis.String(conn.Do(`GET`,`email_key`))


解决方案

使用 gob base64 可以解决问题,例如:



<$
encoding / base64
encoding / gob
bytes


类型SX map [string] interface {}

//二进制编码器
func ToGOB64(m SX)字符串{
b:= bytes.Buffer {}
e: = gob.NewEncoder(& b)
err:= e.En代码(m)
if err!= nil {fmt.Println(`failed gob Encode`,err)}
return base64.StdEncoding.EncodeToString(b.Bytes())
}

//去二进制解码器
func FromGOB64(str string)SX {
m:= SX {}
by,err:= base64.StdEncoding.DecodeString(str)
if err!= nil {fmt.Println(`failed base64 Decode`,err); }
b:= bytes.Buffer {}
b.Write(by)
d:= gob.NewDecoder(&b)
err = d.Decode(& m)
if err!= nil {fmt.Println(`failed gob Decode`,err); }
return m
}

当您需要序列化自定义结构或类型(例如 Session struct),只需添加以下几行:

  func init(){
gob.Register(SX {})
gob.Register(Session {})
}


What's the best way (completeness and performance) in Golang to serialize and deserialize a struct to string and vice versa?

for example, if I have this struct:

struct Session {
   Properties map[string]interface{}
   Permissions []int64
}

I want to store it on Redis and fetch it back. I have tried to save, int and string, it's fine, but how to store struct object?

conn := redisConnectors.Get()

// set example

_, err := conn.Do(`SETEX`, `uid_key`, EXPIRE_SEC, user_id)
_, err = conn.Do(`SETEX`, `email_key`, EXPIRE_SEC, login_email)

// get example

user_id, err := redis.Int64(conn.Do(`GET`, `uid_key`))
login_email, err := redis.String(conn.Do(`GET`, `email_key`))

解决方案

Using gob and base64 could solve the problem, for example:

import (
    "encoding/base64"
    "encoding/gob"
    "bytes"
)

type SX map[string]interface{}

// go binary encoder
func ToGOB64(m SX) string {
    b := bytes.Buffer{}
    e := gob.NewEncoder(&b)
    err := e.Encode(m)
    if err != nil { fmt.Println(`failed gob Encode`, err) }
    return base64.StdEncoding.EncodeToString(b.Bytes())
}

// go binary decoder
func FromGOB64(str string) SX {
    m := SX{}
    by, err := base64.StdEncoding.DecodeString(str)
    if err != nil { fmt.Println(`failed base64 Decode`, err); }
    b := bytes.Buffer{}
    b.Write(by)
    d := gob.NewDecoder(&b)
    err = d.Decode(&m)
    if err != nil { fmt.Println(`failed gob Decode`, err); }
    return m
}

and when you need to serialize custom struct or type (for example Session struct), just add these lines:

func init() {
    gob.Register(SX{})
    gob.Register(Session{}) 
}

这篇关于Golang序列化和反序列化回来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆