golang god

栏目: Go · 发布时间: 7年前

package main import ( "bytes" "encoding/gob" "log" ) func main() { //知识背景 序列化 //golang可以通过json或gob来序列化struct对象,虽然json的序列化更为通用,但利用gob编码可以实现json所不能支持的struct的方法序列化,利用gob包序列化struct保存到本地也十分简单 //gob和json的pack之类的方法一样,由发送端使用Encoder对数据结构进行编码。在接收端收到消息之后,接收端使用Decoder将序列化的数据变化成本地变量。 //rpc remote 场景 var str string = "xiaochuan" e, err := Encode(str) if err != nil { log.Println(err.Error()) } log.Println(string(e)) //强制转换 //new //官方文档 // The new built-in function allocates memory. The first argument is a type, // not a value, and the value returned is a pointer to a newly // allocated zero value of that type. //func new(Type) *Type info_new := new(string) err1 := Decode(e, info_new) if err1 != nil { log.Println(err1.Error()) } log.Println(*info_new) } // 用gob进行数据编码 func Encode(data interface{}) ([]byte, error) { buf := bytes.NewBuffer(nil) enc := gob.NewEncoder(buf) err := enc.Encode(data) if err != nil { return nil, err } return buf.Bytes(), nil } // 用gob进行数据解码 func Decode(data []byte, to interface{}) error { //inteface 万能型的class , 非常开心,兴趣是最大的动力 buf := bytes.NewBuffer(data) dec := gob.NewDecoder(buf) return dec.Decode(to) } END.


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Essential ActionScript 3.0

Essential ActionScript 3.0

Colin Moock / Adobe Dev Library / June 22, 2007 / $34.64

ActionScript 3.0 is a huge upgrade to Flash's programming language. The enhancements to ActionScript's performance, feature set, ease of use, cleanliness, and sophistication are considerable. Essentia......一起来看看 《Essential ActionScript 3.0》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

html转js在线工具
html转js在线工具

html转js在线工具