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


Golang gob.NewDecoder函數代碼示例

本文整理匯總了Golang中encoding/gob.NewDecoder函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewDecoder函數的具體用法?Golang NewDecoder怎麽用?Golang NewDecoder使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了NewDecoder函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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(&gt)
	assert.T(t, err == nil, err)

	var gt2 GobTest
	err = gob.NewDecoder(&buf2).Decode(&gt2)
	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


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