本文整理汇总了Golang中encoding/gob.NewDecoder函数的典型用法代码示例。如果您正苦于以下问题:Golang NewDecoder函数的具体用法?Golang NewDecoder怎么用?Golang NewDecoder使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewDecoder函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: receiveDecrypt
func receiveDecrypt(conn net.Conn) (Message, error) {
// Our work is:
// (receive) -> [de-GOB] -> [DECRYPT] -> [de-GOB] -> msg
// Receive data and de-serialize to get the encrypted message
encMsg := new([]byte)
receive := gob.NewDecoder(conn)
if err := receive.Decode(encMsg); err != nil {
return Message{}, err
}
// Create decrypter and pass it the encrypted message
r := bytes.NewReader(*encMsg)
decrypter, err := saltsecret.NewReader(r, conf.Key, saltsecret.DECRYPT, false)
if err != nil {
return Message{}, err
}
// Read unencrypted serialized message and de-serialize it
msg := new(Message)
dec := gob.NewDecoder(decrypter)
if err = dec.Decode(msg); err != nil {
return Message{}, err
}
return *msg, nil
}
开发者ID:andmarios,项目名称:bashistdb,代码行数:27,代码来源:encryptcomm.go
示例2: ReadFromDisk
func ReadFromDisk() {
file, err := os.Open("data/datafile.gob")
if err == nil {
dec := gob.NewDecoder(file)
err = dec.Decode(&data)
file.Close()
if err != nil {
log.Fatal("Unable to decode:", err)
}
}
var packet WritePacket
for ii := 0; ii < 128; ii++ {
file, err := os.Open("data/datafile.log." + fmt.Sprintf("%03d", ii))
if err != nil {
break
} else {
dec := gob.NewDecoder(file)
for dec.Decode(&packet) == nil {
data[packet.Key] = packet.Val
changeCount++
}
file.Close()
}
}
}
开发者ID:jcamins,项目名称:jcdbp,代码行数:25,代码来源:datastore.go
示例3: TestJsonHelperGobEncoding
func TestJsonHelperGobEncoding(t *testing.T) {
raw := `{"testing": 123,"name":"bob & more"}`
reader := strings.NewReader(raw)
jh, err := NewJsonHelperReader(reader)
assert.Tf(t, err == nil, "Unexpected error decoding gob: %s", err)
assert.Tf(t, jh.Int("testing") == 123, "Unexpected value in gob: %d", jh.Int("testing"))
var buf bytes.Buffer
err = gob.NewEncoder(&buf).Encode(&jh)
assert.T(t, err == nil, err)
var jhNew JsonHelper
err = gob.NewDecoder(&buf).Decode(&jhNew)
assert.T(t, err == nil, err)
assert.Tf(t, jhNew.Int("testing") == 123, "Unexpected value in gob: %d", jhNew.Int("testing"))
assert.Tf(t, jhNew.String("name") == "bob & more", "Unexpected value in gob: %d", jhNew.String("name"))
buf2 := bytes.Buffer{}
gt := GobTest{"Hello", jh}
err = gob.NewEncoder(&buf2).Encode(>)
assert.T(t, err == nil, err)
var gt2 GobTest
err = gob.NewDecoder(&buf2).Decode(>2)
assert.T(t, err == nil, err)
assert.Tf(t, gt2.Name == "Hello", "Unexpected value in gob: %d", gt2.Name)
assert.Tf(t, gt2.Data.Int("testing") == 123, "Unexpected value in gob: %d", gt2.Data.Int("testing"))
assert.Tf(t, gt2.Data.String("name") == "bob & more", "Unexpected value in gob: %d", gt2.Data.String("name"))
}
开发者ID:admpub,项目名称:gou,代码行数:28,代码来源:jsonhelper_test.go
示例4: DecodeValue
func (gobCoder) DecodeValue(r io.Reader, dst reflect.Value) error {
if _, ok := r.(io.ByteReader); !ok {
return gob.NewDecoder(byteReader{r}).DecodeValue(dst)
}
return gob.NewDecoder(r).DecodeValue(dst)
}
开发者ID:shaladdle,项目名称:goaaw,代码行数:7,代码来源:coders.go
示例5: Decode
func (gobCoder) Decode(r io.Reader, dst interface{}) error {
if _, ok := r.(io.ByteReader); !ok {
return gob.NewDecoder(byteReader{r}).Decode(dst)
}
return gob.NewDecoder(r).Decode(dst)
}
开发者ID:shaladdle,项目名称:goaaw,代码行数:7,代码来源:coders.go
示例6: GetGo
func (cursor *Cursor) GetGo(set_key interface{}, op uint, key, val interface{}) error {
var err error
var bset_key []byte
if set_key != nil {
var key_buffer bytes.Buffer
encoder := gob.NewEncoder(&key_buffer)
err = encoder.Encode(set_key)
if err != nil {
return err
}
bset_key = key_buffer.Bytes()
}
bkey, bval, err := cursor.Get(bset_key, op)
if err != nil {
return err
}
buf := bytes.NewReader(bkey)
decoder := gob.NewDecoder(buf)
err = decoder.Decode(key)
if err != nil {
return err
}
buf = bytes.NewReader(bval)
decoder = gob.NewDecoder(buf)
err = decoder.Decode(val)
if err != nil {
return err
}
return nil
}
开发者ID:benbjohnson,项目名称:gomdb,代码行数:30,代码来源:cursor.go
示例7: Load
// Load returns a *fuzz.fuzz.FuzzReport that corresponds to the passed in revision,
// and the fuzz names associated with the report.
// It returns an error if such a Report does not exist.
func (b *FuzzReportCache) Load(revision string) (*fuzz.FuzzReportTree, []string, error) {
var report fuzz.FuzzReportTree
var binaryFuzzNames []string
loadFunc := func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(revision))
if b == nil {
return fmt.Errorf("Cache for revision %s does not exist", revision)
}
c := b.Get(REPORT_KEY)
if c == nil {
return fmt.Errorf("Could not find report for revision %s", revision)
}
dec := gob.NewDecoder(bytes.NewBuffer(c))
if err := dec.Decode(&report); err != nil {
return fmt.Errorf("Could not decode report: %s", err)
}
c = b.Get(BINARY_FUZZES_KEY)
if c == nil {
return fmt.Errorf("Could not find stored binary fuzzes for revision %s", revision)
}
dec = gob.NewDecoder(bytes.NewBuffer(c))
if err := dec.Decode(&binaryFuzzNames); err != nil {
return fmt.Errorf("Could not decode binaryFuzzNames: %s", err)
}
return nil
}
return &report, binaryFuzzNames, b.DB.View(loadFunc)
}
开发者ID:saltmueller,项目名称:skia-buildbot,代码行数:32,代码来源:fuzzcache.go
示例8: appendEntries
func (log *Log) appendEntries(s *Server, prevIndex int64, logEntries []*LogEntry) {
log.Lock()
defer log.Unlock()
if len(log.entries) == 0 {
log.entries = logEntries
return
}
log.entries = log.entries[:prevIndex-log.startIndex()+1]
// log.entries = append(log.entries[:(index-log.startIndex())+1], logEntries...)
for _, entry := range logEntries {
if entry.GetCommandName() == cOldNewStr {
nodes := []Node{}
if err := gob.NewDecoder(bytes.NewReader(entry.GetCommand())).Decode(&nodes); err != nil {
logger.Println("decode Cold,new config err: ", err.Error())
}
s.config.setState(cOldNew)
s.config.cNewNode = make(nodeMap)
for _, node := range nodes {
s.config.cNewNode[node.id()] = node
}
} else if entry.GetCommandName() == cNewStr {
nodes := []Node{}
if err := gob.NewDecoder(bytes.NewReader(entry.GetCommand())).Decode(&nodes); err != nil {
logger.Println("decode Cnew config err: ", err.Error())
}
s.config.setState(cOld)
s.config.cOldNode = makeNodeMap(nodes...)
s.config.cNewNode = nil
}
log.entries = append(log.entries, entry)
}
}
开发者ID:se77en,项目名称:lilraft,代码行数:32,代码来源:log.go
示例9: PostRead
func (zip GobMeddler) PostRead(fieldAddr, scanTarget interface{}) error {
ptr := scanTarget.(*[]byte)
if ptr == nil {
return fmt.Errorf("GobMeddler.PostRead: nil pointer")
}
raw := *ptr
if zip {
// un-gzip and decode gob
gzipReader, err := gzip.NewReader(bytes.NewReader(raw))
if err != nil {
return fmt.Errorf("Error creating gzip Reader: %v", err)
}
defer gzipReader.Close()
gobDecoder := gob.NewDecoder(gzipReader)
if err := gobDecoder.Decode(fieldAddr); err != nil {
return fmt.Errorf("Gob decoder/gzip error: %v", err)
}
if err := gzipReader.Close(); err != nil {
return fmt.Errorf("Closing gzip reader: %v", err)
}
return nil
}
// decode gob
gobDecoder := gob.NewDecoder(bytes.NewReader(raw))
if err := gobDecoder.Decode(fieldAddr); err != nil {
return fmt.Errorf("Gob decode error: %v", err)
}
return nil
}
开发者ID:RidleyLarsen,项目名称:codegrinder,代码行数:33,代码来源:meddler.go
示例10: getData
func (this *DB) getData(bucket *bolt.Bucket) (map[string]int, ipQueue, error) {
var addrQ ipQueue
heapRaw := bucket.Get(boltAddrHeapKey)
if heapRaw == nil {
addrQ = ipQueue(make([]*ipEntry, 0, 1))
} else {
dec := gob.NewDecoder(bytes.NewReader(heapRaw))
if err := dec.Decode(&addrQ); err != nil {
return nil, nil, err
}
}
var addrMap map[string]int
mapRaw := bucket.Get(boltAddrMapKey)
if mapRaw == nil {
addrMap = make(map[string]int)
} else {
dec := gob.NewDecoder(bytes.NewReader(mapRaw))
if err := dec.Decode(&addrMap); err != nil {
return nil, nil, err
}
}
return addrMap, addrQ, nil
}
开发者ID:mbrodala,项目名称:otto,代码行数:25,代码来源:db.go
示例11: read
func (self *GlobalState) read(r *os.File) error {
// skip the version
reader := bufio.NewReader(r)
// read the version line
_, err := reader.ReadString('\n')
if err != nil {
return err
}
err = gob.NewDecoder(reader).Decode(self)
// from version 0.7 to 0.8 the type of the Suffix variables
// changed to uint32. Catch this and convert to a new GlobalState object.
if err != nil {
old := &oldGlobalState{}
r.Seek(int64(0), 0)
reader := bufio.NewReader(r)
// read the version line
_, err := reader.ReadString('\n')
if err != nil {
return err
}
err = gob.NewDecoder(reader).Decode(old)
if err != nil {
return err
}
self.CurrentFileOffset = old.CurrentFileOffset
self.CurrentFileSuffix = uint32(old.CurrentFileSuffix)
self.LargestRequestNumber = old.LargestRequestNumber
self.FirstSuffix = uint32(old.FirstSuffix)
self.ShardLastSequenceNumber = old.ShardLastSequenceNumber
self.ServerLastRequestNumber = old.ServerLastRequestNumber
}
return nil
}
开发者ID:Kenterfie,项目名称:influxdb,代码行数:34,代码来源:global_state.go
示例12: loadDirectory
func (db *dbImpl) loadDirectory() (*directory, error) {
dir := &directory{
Usernames: make(map[uID]string),
AIs: make(map[aiID]*aiInfo),
AIStats: make(map[aiID]*aiStats),
}
err := db.View(func(tx *bolt.Tx) error {
// Load AIs
b := tx.Bucket(AIBucket)
err := b.ForEach(func(k, v []byte) error {
var a aiInfo
buf := bytes.NewReader(v)
dec := gob.NewDecoder(buf)
if err := dec.Decode(&a); err != nil {
return err
}
dir.AIs[a.ID] = &a
return nil
})
if err != nil {
return err
}
// Load AIStats
b = tx.Bucket(AIStatsBucket)
err = b.ForEach(func(k, v []byte) error {
var a aiStats
buf := bytes.NewReader(v)
dec := gob.NewDecoder(buf)
if err := dec.Decode(&a); err != nil {
return err
}
dir.AIStats[aiID(k)] = &a
return nil
})
if err != nil {
return err
}
// Load usernames
b = tx.Bucket(UserBucket)
return b.ForEach(func(k, v []byte) error {
var u userInfo
buf := bytes.NewReader(v)
dec := gob.NewDecoder(buf)
if err := dec.Decode(&u); err != nil {
return err
}
dir.Usernames[u.ID] = u.Name
return nil
})
})
return dir, err
}
开发者ID:bcspragu,项目名称:Gobots,代码行数:57,代码来源:datastore.go
示例13: NewGobConnection
func NewGobConnection(conn net.Conn) GobConnection {
if gcn, ok := gobPool.Get().(*gobConnection); ok {
gcn.rwc = conn
gcn.enc = gob.NewEncoder(conn)
gcn.dec = gob.NewDecoder(conn)
return gcn
}
return &gobConnection{rwc: conn, enc: gob.NewEncoder(conn), dec: gob.NewDecoder(conn)}
}
开发者ID:dongjun111111,项目名称:notes,代码行数:9,代码来源:小工具_一个基于Gob的TCP通讯包.go
示例14: NewGobCodecs
func NewGobCodecs(conn io.ReadWriteCloser) (*gobServerCodec, *gobClientCodec) {
sbuf := bufio.NewWriter(conn)
srv := &gobServerCodec{conn, gob.NewDecoder(conn), gob.NewEncoder(sbuf), sbuf}
cbuf := bufio.NewWriter(conn)
cli := &gobClientCodec{conn, gob.NewDecoder(conn), gob.NewEncoder(cbuf), cbuf}
return srv, cli
}
开发者ID:vgp,项目名称:gogp,代码行数:9,代码来源:codec.go
示例15: TestPublisherClosesDuplicateConnections
func TestPublisherClosesDuplicateConnections(t *testing.T) {
log.SetOutput(ioutil.Discard)
// Choose a port
port, err := getFreePort()
if err != nil {
t.Fatal(err)
}
// Start a publisher
p, err := xfer.NewTCPPublisher(port)
if err != nil {
t.Fatal(err)
}
defer p.Close()
// Connect a listener
conn, err := net.Dial("tcp4", "127.0.0.1"+port)
if err != nil {
t.Fatal(err)
}
defer conn.Close()
if err := gob.NewEncoder(conn).Encode(xfer.HandshakeRequest{ID: "foo"}); err != nil {
t.Fatal(err)
}
time.Sleep(time.Millisecond)
// Try to connect the same listener
dupconn, err := net.Dial("tcp4", "127.0.0.1"+port)
if err != nil {
t.Fatal(err)
}
// Send handshake
if err := gob.NewEncoder(dupconn).Encode(xfer.HandshakeRequest{ID: "foo"}); err != nil {
t.Fatal(err)
}
defer dupconn.Close()
// Publish a message
p.Publish(report.Report{})
// The first listener should receive it
var r report.Report
if err := gob.NewDecoder(conn).Decode(&r); err != nil {
t.Fatal(err)
}
// The duplicate listener should have an error
if err := gob.NewDecoder(dupconn).Decode(&r); err == nil {
t.Errorf("expected error, got none")
} else {
t.Logf("dupconn got expected error: %v", err)
}
}
开发者ID:neviim,项目名称:scope,代码行数:54,代码来源:publisher_test.go
示例16: Init
func (c *Connection) Init() {
c.send_buffer = new(bytes.Buffer)
c.recv_buffer = new(bytes.Buffer)
c.send_buffer_decoder = gob.NewDecoder(c.send_buffer)
c.send_buffer_encoder = gob.NewEncoder(c.send_buffer)
c.recv_buffer_decoder = gob.NewDecoder(c.recv_buffer)
c.recv_buffer_encoder = gob.NewEncoder(c.recv_buffer)
c.Quit = make(chan bool)
c.Outgoing = make(chan interface{}, 1000)
}
开发者ID:KingsEpic,项目名称:client,代码行数:11,代码来源:connection.go
示例17: bytesToObject
func bytesToObject(v interface{}, b []byte) error {
f := flate.NewReader(bytes.NewReader(b))
defer f.Close()
err := gob.NewDecoder(f).Decode(v)
if err != nil {
// try the old, uncompressed format.
err = gob.NewDecoder(bytes.NewReader(b)).Decode(v)
}
return err
}
开发者ID:BenLubar,项目名称:untitled-game,代码行数:11,代码来源:world.go
示例18: Next
func (it *Iterator) Next(imm interface{}) (*KeyType, bool) {
if it == nil {
return nil, false
}
if it.i >= len(it.entries) {
it.entries = nil
return nil, false
}
entry := it.entries[it.i]
// immutable
buff := bytes.NewBuffer(entry.imm.Val)
dec := gob.NewDecoder(buff)
if err := dec.Decode(imm); err != nil {
fmt.Println(err)
return nil, false
}
it.i++
m, ok := imm.(Mutable)
if ok {
mut := m.Mutable()
if mut == nil {
return entry.kt, true
}
if entry.mut == nil {
panic("there should be something here!")
return entry.kt, true
}
mutv := reflect.ValueOf(mut)
if err := validateMutableEntry(mutv); err != nil {
return nil, false
}
// update the v with the new pointer value
entry.mut.v = mutv
// mutable
buff = bytes.NewBuffer(entry.mut.Val)
dec = gob.NewDecoder(buff)
if err := dec.Decode(mut); err != nil {
fmt.Println(err)
return nil, false
}
}
return entry.kt, true
}
开发者ID:paddie,项目名称:statedb,代码行数:53,代码来源:restore.go
示例19: AddEntry
func (d *FileTreeCache) AddEntry(entry protocol.FileInfo, peer protocol.DeviceID) {
d.db.Update(func(tx *bolt.Tx) error {
eb := tx.Bucket(d.folderBucketKey).Bucket(entriesBucket)
/* save entry */
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
enc.Encode(entry)
eb.Put([]byte(entry.Name), buf.Bytes()) // TODO handle error?
/* add peer */
edb := tx.Bucket(d.folderBucketKey).Bucket(entryDevicesBucket)
v := edb.Get([]byte(entry.Name))
var devices map[string]bool
if v == nil {
devices = make(map[string]bool)
} else {
rbuf := bytes.NewBuffer(v)
dec := gob.NewDecoder(rbuf)
dec.Decode(&devices)
}
devices[peer.String()] = true
var dbuf bytes.Buffer
enc = gob.NewEncoder(&dbuf)
enc.Encode(devices)
edb.Put([]byte(entry.Name), dbuf.Bytes())
/* add child lookup */
dir := path.Dir(entry.Name)
clb := tx.Bucket(d.folderBucketKey).Bucket(childLookupBucket)
v = clb.Get([]byte(dir))
if debug {
l.Debugln("Adding child", entry.Name, "for dir", dir)
}
var children map[string]bool
if v == nil {
children = make(map[string]bool)
} else {
rbuf := bytes.NewBuffer(v)
dec := gob.NewDecoder(rbuf)
dec.Decode(&children)
}
children[entry.Name] = true
var cbuf bytes.Buffer
enc = gob.NewEncoder(&cbuf)
enc.Encode(children)
clb.Put([]byte(dir), cbuf.Bytes())
return nil
})
}
开发者ID:burkemw3,项目名称:syncthingfuse,代码行数:53,代码来源:filetreecache.go
示例20: NewConsumer
// NewConsumer return a pointer of Consumer instance,
// compression is a optional, it can be true if need zlib compression
// else false.
func NewConsumer(conn net.Conn, compression bool) (*Consumer, error) {
if !compression {
dec := gob.NewDecoder(conn)
return &Consumer{conn, dec, nil}, nil
}
compR, err := zlib.NewReader(conn)
if err != nil {
return nil, err
}
dec := gob.NewDecoder(compR)
return &Consumer{conn, dec, compR}, nil
}
开发者ID:houcy,项目名称:thriftAndGob,代码行数:16,代码来源:consumer.go
注:本文中的encoding/gob.NewDecoder函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论