本文整理汇总了Golang中encoding/gob.Encoder类的典型用法代码示例。如果您正苦于以下问题:Golang Encoder类的具体用法?Golang Encoder怎么用?Golang Encoder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Encoder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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())
}
}
}
示例2: SendPeer
func SendPeer(encod *gob.Encoder) {
err := encod.Encode(*MyPublicPeer)
if err != nil {
log.Fatal("Error sending Public Peer: ", err)
}
}
示例3: sendMakeDirectory
func sendMakeDirectory(encoder *gob.Encoder, directory image.Directory) error {
imageUpdate := imageserver.ImageUpdate{
Directory: &directory,
Operation: imageserver.OperationMakeDirectory,
}
return encoder.Encode(imageUpdate)
}
示例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
}
示例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)
}
}
}
示例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
}
示例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())
}
}
}
示例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
}
}
}
}
示例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
}
示例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
}
示例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)
}
示例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
}
示例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)
}
}
}
示例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
}
示例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
}