本文整理汇总了Golang中encoding/gob.NewEncoder函数的典型用法代码### 示例。如果您正苦于以下问题:Golang NewEncoder函数的具体用法?Golang NewEncoder怎么用?Golang NewEncoder使用的例子?那么恭喜您, 这里精选的函数代码### 示例或许可以为您提供帮助。
在下文中一共展示了NewEncoder函数的20个代码### 示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码### 示例。
示例1: encryptDispatch
func encryptDispatch(conn net.Conn, m Message) error {
// We want to sent encrypted data.
// In order to encrypt, we need to first serialize the message.
// In order to sent/receive hassle free, we need to serialize the encrypted message
// So: msg -> [GOB] -> [ENCRYPT] -> [GOB] -> (dispatch)
// Create encrypter
var encMsg bytes.Buffer
encrypter, err := saltsecret.NewWriter(&encMsg, conf.Key, saltsecret.ENCRYPT, true)
if err != nil {
return err
}
// Serialize message
enc := gob.NewEncoder(encrypter)
if err = enc.Encode(m); err != nil {
return err
}
// Flush encrypter to actuall encrypt the message
if err = encrypter.Flush(); err != nil {
return err
}
// Serialize encrypted message and dispatch it
dispatch := gob.NewEncoder(conn)
if err = dispatch.Encode(encMsg.Bytes()); err != nil {
return err
}
return nil
}
开发者ID:andmarios,项目名称:bashistdb,代码行数:32,代码来源:encryptcomm.go
示例2: GobEncode
// When checkpointing the system, encode the non-public interface into the Val,
// followed by a normal encoding of the struct
func (m *MutState) GobEncode() ([]byte, error) {
if !m.v.IsValid() {
return nil, fmt.Errorf("Trying to checkpoint a mutable state with a pointer from a previous ceckpoint")
}
var b bytes.Buffer
enc := gob.NewEncoder(&b)
if err := enc.EncodeValue(m.v.Elem()); err != nil {
return nil, err
}
m.Val = b.Bytes()
var buff bytes.Buffer
enc = gob.NewEncoder(&buff)
if err := enc.Encode(m.KT); err != nil {
return nil, err
}
if err := enc.Encode(m.Val); err != nil {
return nil, err
}
return buff.Bytes(), nil
}
开发者ID:paddie,项目名称:statedb,代码行数:27,代码来源:mutable.go
示例3: sendWork
func sendWork(c net.Conn, workchan chan Work, clientId int) {
for {
// assign works until workchan close
select {
case work, ok := <-workchan:
if ok {
fmt.Printf("[INFO]Assigned work to [%d-%d] to client %d\n", work.StartVal, work.EndVal, clientId)
encoder := gob.NewEncoder(c)
err := encoder.Encode(work)
if err != nil {
fmt.Printf("[ERROR]Client %d disconnected, lost Job [%d-%d]\n", clientId, work.StartVal, work.EndVal)
return
}
} else {
// if workchan is closed, send complete message to worker
encoder := gob.NewEncoder(c)
err := encoder.Encode(Work{-1, 0, "", ""})
if err != nil {
fmt.Printf("[ERROR]Send complete message to client %d failed\n", clientId)
}
return
}
default:
time.Sleep(1 * time.Second)
}
}
}
开发者ID:RayZ-O,项目名称:go-distributed-simulator,代码行数:27,代码来源:server.go
示例4: Store
// Store stores a fuzz.FuzzReport and the binaryFuzzNames associated with it to the underlying
// fuzz.FuzzReportCache. It creates a bucket with the
// name of the given revision and stores the report as a []byte under a simple key.
func (b *FuzzReportCache) Store(report fuzz.FuzzReportTree, binaryFuzzNames []string, revision string) error {
storeFunc := func(tx *bolt.Tx) error {
bkt, err := tx.CreateBucketIfNotExists([]byte(revision))
if err != nil {
return fmt.Errorf("Could not make cache/bucket for %s", revision)
}
var buffReport bytes.Buffer
enc := gob.NewEncoder(&buffReport)
if err := enc.Encode(report); err != nil {
return fmt.Errorf("Problem encoding report: %s", err)
}
if err := bkt.Put(REPORT_KEY, buffReport.Bytes()); err != nil {
return fmt.Errorf("Problem storing %d bytes of report: %s", buffReport.Len(), err)
}
var buffNames bytes.Buffer
enc = gob.NewEncoder(&buffNames)
if err := enc.Encode(binaryFuzzNames); err != nil {
return fmt.Errorf("Problem encoding fuzz names: %s", err)
}
if err := bkt.Put(BINARY_FUZZES_KEY, buffNames.Bytes()); err != nil {
return fmt.Errorf("Problem storing %d bytes of binaryFuzzNames: %s", buffNames.Len(), err)
}
return nil
}
return b.DB.Update(storeFunc)
}
开发者ID:saltmueller,项目名称:skia-buildbot,代码行数:29,代码来源:fuzzcache.go
示例5: PreWrite
func (zip GobMeddler) PreWrite(field interface{}) (saveValue interface{}, err error) {
buffer := new(bytes.Buffer)
if zip {
// gob encode and gzip
gzipWriter := gzip.NewWriter(buffer)
defer gzipWriter.Close()
gobEncoder := gob.NewEncoder(gzipWriter)
if err := gobEncoder.Encode(field); err != nil {
return nil, fmt.Errorf("Gob encoding/gzip error: %v", err)
}
if err := gzipWriter.Close(); err != nil {
return nil, fmt.Errorf("Closing gzip writer: %v", err)
}
return buffer.Bytes(), nil
}
// gob encode
gobEncoder := gob.NewEncoder(buffer)
if err := gobEncoder.Encode(field); err != nil {
return nil, fmt.Errorf("Gob encoding error: %v", err)
}
return buffer.Bytes(), nil
}
开发者ID:RidleyLarsen,项目名称:codegrinder,代码行数:25,代码来源:meddler.go
最后编辑: kuteng 文档更新时间: 2021-08-23 19:14 作者:kuteng