本文整理汇总了Golang中encoding/gob.Encoder类的典型用法代码示例。如果您正苦于以下问题:Golang Encoder类的具体用法?Golang Encoder怎么用?Golang Encoder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Encoder类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: gobDecodeList
func gobDecodeList(b *testing.B, l int) {
var (
enc *gob.Encoder
buf bytes.Buffer
err error
)
s := make([]int, l)
for i, _ := range s {
s[i] = rand.Int()
}
enc = gob.NewEncoder(&buf)
err = enc.Encode(s)
if err != nil {
panic(err.Error())
}
var dest []int
bs := buf.Bytes()
b.ResetTimer()
// decode
for i := 0; i < b.N; i++ {
nbuffer := bytes.NewBuffer(bs)
dec := gob.NewDecoder(nbuffer)
err := dec.Decode(&dest)
if err != nil {
panic(err.Error())
}
}
}
开发者ID:samuelyao314,项目名称:mygo,代码行数:30,代码来源:gob_test.go
示例2: SendPeer
func SendPeer(encod *gob.Encoder) {
err := encod.Encode(*MyPublicPeer)
if err != nil {
log.Fatal("Error sending Public Peer: ", err)
}
}
开发者ID:rcostu,项目名称:cosmofs,代码行数:7,代码来源:peer.go
示例3: sendMakeDirectory
func sendMakeDirectory(encoder *gob.Encoder, directory image.Directory) error {
imageUpdate := imageserver.ImageUpdate{
Directory: &directory,
Operation: imageserver.OperationMakeDirectory,
}
return encoder.Encode(imageUpdate)
}
开发者ID:keep94,项目名称:Dominator,代码行数:7,代码来源:getImageUpdates.go
示例4: netSender
func netSender(netOutStream <-chan HashedInterval, encoder *gob.Encoder, netOutDoneStream chan<- bool) {
for r := range netOutStream {
if verboseServer {
log.Debug("Server.netSender: sending", r.FileInterval)
}
err := encoder.Encode(r)
if err != nil {
log.Fatal("Protocol encoder error:", err)
netOutDoneStream <- false
return
}
}
rEOF := HashedInterval{FileInterval{SparseIgnore, Interval{}}, make([]byte, 0)}
if rEOF.Len() != 0 {
log.Fatal("Server.netSender internal error")
}
// err := encoder.Encode(HashedInterval{FileInterval{}, make([]byte, 0)})
err := encoder.Encode(rEOF)
if err != nil {
log.Fatal("Protocol encoder error:", err)
netOutDoneStream <- false
return
}
if verboseServer {
log.Debug("Server.netSender: finished sending hashes")
}
netOutDoneStream <- true
}
开发者ID:th3architect,项目名称:longhorn,代码行数:29,代码来源:server.go
示例5: CreateGobsFile
func CreateGobsFile(targetFilePath string, recs *[]interface{}, getRecPtr GobRecPtrMaker, gzipped bool) {
var file, err = os.Create(targetFilePath)
var gobber *gob.Encoder
var gzipper *gzip.Writer
if file != nil {
defer file.Close()
}
if err != nil {
panic(err)
}
if gzipped {
if gzipper, err = gzip.NewWriterLevel(file, gzip.BestCompression); gzipper != nil {
defer gzipper.Close()
gobber = gob.NewEncoder(gzipper)
}
if err != nil {
panic(err)
}
} else {
gobber = gob.NewEncoder(file)
}
for _, rec := range *recs {
if err = gobber.Encode(coreutil.PtrVal(getRecPtr(rec))); err != nil {
panic(err)
}
}
}
开发者ID:hyl87,项目名称:2011_Go_Geo_Gfx,代码行数:27,代码来源:gobutil.go
示例6: exchangeHandshake
func (conn *LocalConnection) exchangeHandshake(localConnID uint64, usingPassword bool, enc *gob.Encoder, dec *gob.Decoder) (*FieldValidator, *[32]byte, error) {
handshakeSend := map[string]string{
"PeerNameFlavour": PeerNameFlavour,
"Name": conn.local.Name.String(),
"NickName": conn.local.NickName,
"UID": fmt.Sprint(conn.local.UID),
"ConnID": fmt.Sprint(localConnID)}
handshakeRecv := map[string]string{}
var public, private *[32]byte
var err error
if usingPassword {
public, private, err = GenerateKeyPair()
if err != nil {
return nil, nil, err
}
handshakeSend["PublicKey"] = hex.EncodeToString(public[:])
}
enc.Encode(handshakeSend)
if err = dec.Decode(&handshakeRecv); err != nil {
return nil, nil, err
}
fv := NewFieldValidator(handshakeRecv)
fv.CheckEqual("PeerNameFlavour", PeerNameFlavour)
return fv, private, nil
}
开发者ID:rahulxkrishna,项目名称:weave,代码行数:25,代码来源:handshake.go
示例7: gobDecodeMap
func gobDecodeMap(b *testing.B, l int, kl int) {
var (
enc *gob.Encoder
buf bytes.Buffer
err error
)
s := make(map[string]int)
for i := 0; i < l; i++ {
key := keygen(i, kl)
s[key] = rand.Int()
}
enc = gob.NewEncoder(&buf)
err = enc.Encode(s)
if err != nil {
panic(err.Error())
}
var dest map[string]int
bs := buf.Bytes()
b.ResetTimer()
// decode
for i := 0; i < b.N; i++ {
nbuffer := bytes.NewBuffer(bs)
dec := gob.NewDecoder(nbuffer)
err := dec.Decode(&dest)
if err != nil {
panic(err.Error())
}
}
}
开发者ID:samuelyao314,项目名称:mygo,代码行数:32,代码来源:gob_test.go
示例8: readinput
func readinput(c net.Conn, enc *gob.Encoder) {
var buf [1]byte
for {
n, err := os.Stdin.Read(buf[:])
if err != nil {
log.Print("Failed to read stdin.")
return
}
if n != 1 {
// short read? why?
continue
}
if buf[0] == 0x01 { // ctrl-a
ctrla := escape(c, enc)
if !ctrla {
continue
}
}
if readwrite {
req := &connReq{Cmd: Write, Input: buf[:]}
err = enc.Encode(req)
if err != nil {
log.Print("Failed to write on connection.")
// just keep trying
}
}
}
}
开发者ID:jmptrader,项目名称:jra-go,代码行数:30,代码来源:client.go
示例9: encodeCacheToGob
func (cbv *CachedBackendVersion) encodeCacheToGob(enc *gob.Encoder) error {
cbv.mu.Lock()
defer cbv.mu.Unlock()
if err := enc.Encode(cbv.cache); err != nil {
return fmt.Errorf("Failed to encode cache map: %v", err)
}
return nil
}
开发者ID:postfix,项目名称:otaru,代码行数:9,代码来源:cachedbackendversion.go
示例10: safeEncode
func (m *gobMarshallingProvider) safeEncode(enc *gob.Encoder, val interface{}) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("Panic encoding: %v", r)
}
}()
err = enc.Encode(val)
return
}
开发者ID:antongulenko,项目名称:RTP,代码行数:9,代码来源:packet.go
示例11: Save
func (console *Console) Save(encoder *gob.Encoder) error {
encoder.Encode(console.RAM)
console.CPU.Save(encoder)
console.APU.Save(encoder)
console.PPU.Save(encoder)
console.Cartridge.Save(encoder)
console.Mapper.Save(encoder)
return encoder.Encode(true)
}
开发者ID:cherrybob,项目名称:nes,代码行数:9,代码来源:console.go
示例12: writeTo
func writeTo(buffers [][]*rfc5424.Message, enc *gob.Encoder) error {
for _, buf := range buffers {
for _, msg := range buf {
if err := enc.Encode(msg); err != nil {
return err
}
}
}
return nil
}
开发者ID:devick,项目名称:flynn,代码行数:10,代码来源:snapshot.go
示例13: clientEncode
func clientEncode(encode *gob.Encoder, buf *bufio.Writer, send <-chan *packet.Packet) {
for msg := range send {
if err := encode.Encode(msg); err != nil {
log.Printf("Error encoding packet: %s", err)
}
if err := buf.Flush(); err != nil {
log.Printf("Error writing buffer: %s", err)
}
}
}
开发者ID:Nightgunner5,项目名称:gogame,代码行数:10,代码来源:client.go
示例14: EncodeToGob
func (dn *DirNode) EncodeToGob(enc *gob.Encoder) error {
if err := serializeCommon(enc, dn.GetType(), dn.INodeCommon); err != nil {
return err
}
if err := enc.Encode(dn.Entries); err != nil {
return fmt.Errorf("Failed to encode Entries: %v", err)
}
return nil
}
开发者ID:hajimehoshi,项目名称:otaru,代码行数:11,代码来源:snapshot.go
示例15: serializeStructInternalEncode
// serializeStructInternalEncode takes struct field metadata and encodes the value using gob.
func serializeStructInternalEncode(enc *gob.Encoder, smd *structMetaData, fieldName, metaData string, encodeValue bool, v reflect.Value) error {
smd.metaDatas = append(smd.metaDatas, metaData)
smd.totalLength += len(metaData)
if encodeValue {
if err := enc.EncodeValue(v); err != nil {
return fmt.Errorf("goon: Failed to encode field %v value %v - %v", fieldName, v.Interface(), err)
}
}
return nil
}
开发者ID:laco0416,项目名称:goon,代码行数:12,代码来源:entity.go
示例16: Save
func (m *Mapper1) Save(encoder *gob.Encoder) error {
encoder.Encode(m.shiftRegister)
encoder.Encode(m.control)
encoder.Encode(m.prgMode)
encoder.Encode(m.chrMode)
encoder.Encode(m.prgBank)
encoder.Encode(m.chrBank0)
encoder.Encode(m.chrBank1)
encoder.Encode(m.prgOffsets)
encoder.Encode(m.chrOffsets)
return nil
}
开发者ID:cherrybob,项目名称:nes,代码行数:12,代码来源:mapper1.go
示例17: negotiateVersion
func negotiateVersion(e *gob.Encoder, d *gob.Decoder) {
var c Command
c.Command = CmdVersion
c.Params = []string{protocolVersion}
err := e.Encode(c)
panicOn(err)
err = d.Decode(&c)
if c.Params[0] != protocolVersion {
panic(fmt.Errorf("Mismatched protocol version %s != %s", c.Params[0], protocolVersion))
}
panicOn(err)
}
开发者ID:carriercomm,项目名称:zsync,代码行数:12,代码来源:main.go
示例18: interfaceEncode
// interfaceEncode encodes the interface value into the encoder.
func interfaceEncode(enc *gob.Encoder, p Pythagoras) {
// The encode will fail unless the concrete type has been
// registered. We registered it in the calling function.
// Pass pointer to interface so Encode sees (and hence sends) a value of
// interface type. If we passed p directly it would see the concrete type instead.
// See the blog post, "The Laws of Reflection" for background.
err := enc.Encode(&p)
if err != nil {
log.Fatal("encode:", err)
}
}
开发者ID:2thetop,项目名称:go,代码行数:13,代码来源:example_interface_test.go
示例19: gobEncode
func gobEncode(data interface{}) {
var (
enc *gob.Encoder
buf bytes.Buffer
err error
)
enc = gob.NewEncoder(&buf)
err = enc.Encode(data)
if err != nil {
panic(err.Error())
}
}
开发者ID:samuelyao314,项目名称:mygo,代码行数:12,代码来源:gob_test.go
示例20: SaveConfig
func (p *IndexDb) SaveConfig(path string, config *IndexDbConfig) error {
var buf bytes.Buffer
var enc *gob.Encoder = gob.NewEncoder(&buf)
var encodeErr error = enc.Encode(config)
if encodeErr != nil {
log.Fatal("encode error:", encodeErr)
}
file, err := os.Create(path)
file.Write(buf.Bytes())
file.Close()
return err
}
开发者ID:c9s,项目名称:golocate,代码行数:13,代码来源:indexdb.go
注:本文中的encoding/gob.Encoder类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论