當前位置: 首頁>>代碼示例>>Golang>>正文


Golang gob.Encoder類代碼示例

本文整理匯總了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())
		}
	}
}
開發者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


注:本文中的encoding/gob.Encoder類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。