本文整理汇总了Golang中gob.NewEncoder函数的典型用法代码示例。如果您正苦于以下问题:Golang NewEncoder函数的具体用法?Golang NewEncoder怎么用?Golang NewEncoder使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewEncoder函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestNodeSnapshotBad
func TestNodeSnapshotBad(t *testing.T) {
buf := bytes.NewBuffer([]byte{})
gob.NewEncoder(buf).Encode(uint64(1))
seqnPart := buf.String()
buf = bytes.NewBuffer([]byte{})
gob.NewEncoder(buf).Encode(emptyDir)
valPart := buf.String()
valPart = valPart[0 : len(valPart)/2]
m := seqnPart + valPart
n, e := emptyDir.apply(1, m)
assert.Equal(t, emptyDir, n)
assert.Equal(t, Event{2, "", "", "", m, io.ErrUnexpectedEOF, n}, e)
}
示例2: GobEncode
func (block *Block) GobEncode() ([]byte, error) {
buffer := bytes.NewBuffer([]byte{})
encoder := gob.NewEncoder(buffer)
var err error
err = encoder.EncodeValue(reflect.ValueOf(gobNodeVersion))
if err != nil {
return nil, err
}
err = encoder.EncodeValue(reflect.ValueOf(&block.position))
if err != nil {
return nil, err
}
encoder.EncodeValue(reflect.ValueOf(&block.weak))
if err != nil {
return nil, err
}
encoder.EncodeValue(reflect.ValueOf(&block.strong))
if err != nil {
return nil, err
}
return buffer.Bytes(), nil
}
示例3: GobEncode
func (k *Key) GobEncode() ([]byte, error) {
buf := new(bytes.Buffer)
if err := gob.NewEncoder(buf).Encode(keyToGobKey(k)); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
示例4: EncipherMsg
// Encipher a message
func EncipherMsg(plaintext []byte, pubkey *CipherMsgPubKey) ([]byte, os.Error) {
msg := &U_CipherMsg{
Text: make([]byte, len(plaintext)),
}
n := copy(msg.Text, plaintext)
if n != len(plaintext) {
panic("crypto, copy text")
}
urand := NewTimedRand()
seed := make([]byte, cipherMsgSeedLen)
n, _ = urand.Read(seed)
if n != len(seed) {
panic("crypto,gen seed")
}
cseed, err := EncryptShortMsg(pubkey.rsa, seed, []byte(""))
if err != nil {
return nil, err
}
msg.Seed = cseed
rc, err := rc4.NewCipher(seed)
if err != nil {
panic("rc4tube")
}
rc.XORKeyStream(msg.Text)
var w bytes.Buffer
enc := gob.NewEncoder(&w)
err = enc.Encode(msg)
if err != nil {
return nil, err
}
return w.Bytes(), nil
}
示例5: handleFreezeRequest
func (server *Server) handleFreezeRequest(freq *freezeRequest, fs *frozenServer) {
pr, pw := io.Pipe()
freq.readCloser = pr
freq.done <- true
zw, err := gzip.NewWriterLevel(pw, gzip.BestCompression)
if err != nil {
if err = pw.CloseWithError(err); err != nil {
log.Panicf("Unable to close PipeWriter: %v", err.String())
}
return
}
enc := gob.NewEncoder(zw)
err = enc.Encode(fs)
if err != nil {
if err = pw.CloseWithError(err); err != nil {
log.Panicf("Unable to close PipeWriter: %v", err.String())
}
}
if err = pw.CloseWithError(zw.Close()); err != nil {
log.Panicf("Unable to close PipeWriter: %v", err.String())
}
}
示例6: RetrieveActDetails
func RetrieveActDetails(c appengine.Context, actId int) (res *ActDetail, err os.Error) {
var d DSActDetail
if itm, err := memcache.Get(c, "actId__"+strconv.Itoa(actId)); err != nil && err != memcache.ErrCacheMiss {
return nil, err
} else if err == nil {
// Cache hit
buf := bytes.NewBuffer(itm.Value)
dec := gob.NewDecoder(buf)
dec.Decode(&d)
} else {
// Cache miss
key := datastore.NewKey(c, "DSActDetail", "", int64(actId), nil)
if err := datastore.Get(c, key, &d); err == datastore.ErrNoSuchEntity {
return &ActDetail{ActId: actId}, nil
} else if err != nil {
return nil, err
}
buf := bytes.NewBufferString("")
enc := gob.NewEncoder(buf)
enc.Encode(d)
itm := &memcache.Item{
Key: "actId__" + strconv.Itoa(actId),
Value: buf.Bytes(),
}
err = memcache.Set(c, itm)
c.Debugf("Request cache to memcache")
}
return d.fromDS(), nil
}
示例7: Bind
func (p QueryParameter) Bind(s *Statement, value interface{}) (e os.Error) {
var rv Errno
switch v := value.(type) {
case nil:
rv = Errno(C.sqlite3_bind_null(s.cptr, C.int(p)))
case int:
rv = Errno(C.sqlite3_bind_int(s.cptr, C.int(p), C.int(v)))
case string:
rv = Errno(C.gosqlite3_bind_text(s.cptr, C.int(p), C.CString(v), C.int(len(v))))
case int64:
rv = Errno(C.sqlite3_bind_int64(s.cptr, C.int(p), C.sqlite3_int64(v)))
case float32:
rv = Errno(C.sqlite3_bind_double(s.cptr, C.int(p), C.double(v)))
case float64:
rv = Errno(C.sqlite3_bind_double(s.cptr, C.int(p), C.double(v)))
default:
buffer := new(bytes.Buffer)
encoder := gob.NewEncoder(buffer)
if encoder.Encode(value) != nil {
rv = ENCODER
} else {
rawbuffer := string(buffer.Bytes())
rv = Errno(C.gosqlite3_bind_blob(s.cptr, C.int(p), unsafe.Pointer(C.CString(rawbuffer)), C.int(len(rawbuffer))))
}
}
if rv != OK {
e = rv
}
return
}
示例8: Save
func (c *FlatHaveCache) Save() {
c.mu.Lock()
defer c.mu.Unlock()
if len(c.dirty) == 0 {
cachelog.Printf("FlatHaveCache: Save, but nothing dirty")
return
}
f, err := os.OpenFile(c.filename, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
if err != nil {
log.Fatalf("FlatHaveCache OpenFile: %v", err)
}
defer f.Close()
e := gob.NewEncoder(f)
write := func(v interface{}) {
if err := e.Encode(v); err != nil {
panic("Encode: " + err.String())
}
}
for k, _ := range c.dirty {
write(k)
}
c.dirty = make(map[string]bool)
cachelog.Printf("FlatHaveCache: saved")
}
示例9: main
func main() {
data, _ := ioutil.ReadFile("/space/raxml/VINCENT/RAxML_bipartitions.1604.BEST.WITH")
n, _ := Parse(StringPI(string(data)), 0)
//n,_ := Parse( StringPI(" (bla:.1,bla2:2.1,(bla32:1.2,bla34:0.7)100:3.4);"), 0 );
PrintTree(n, os.Stdout)
fmt.Println()
PrintTree(n.Back, os.Stdout)
fmt.Println()
i := make([]int, 4)
i[0] = 72
i[1] = 105
i[2] = 32
i[3] = 9731 // Unicode snowman
var s string = string(i)
fmt.Printf("%s has length %d bytes.\n", s, len(s))
buf := bytes.NewBuffer(nil)
enc := gob.NewEncoder(buf)
enc.Encode(n)
os.Stdout.Write(buf.Bytes())
}
示例10: gobMarshal
func gobMarshal(v interface{}) ([]byte, os.Error) {
var buf bytes.Buffer
if err := gob.NewEncoder(&buf).Encode(v); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
示例11: TestRatGobEncoding
func TestRatGobEncoding(t *testing.T) {
var medium bytes.Buffer
enc := gob.NewEncoder(&medium)
dec := gob.NewDecoder(&medium)
for i, test := range gobEncodingTests {
for j := 0; j < 4; j++ {
medium.Reset() // empty buffer for each test case (in case of failures)
stest := test
if j&1 != 0 {
// negative numbers
stest = "-" + test
}
if j%2 != 0 {
// fractions
stest = stest + "." + test
}
var tx Rat
tx.SetString(stest)
if err := enc.Encode(&tx); err != nil {
t.Errorf("#%d%c: encoding failed: %s", i, 'a'+j, err)
}
var rx Rat
if err := dec.Decode(&rx); err != nil {
t.Errorf("#%d%c: decoding failed: %s", i, 'a'+j, err)
}
if rx.Cmp(&tx) != 0 {
t.Errorf("#%d%c: transmission failed: got %s want %s", i, 'a'+j, &rx, &tx)
}
}
}
}
示例12: TestSnapshotBad
func TestSnapshotBad(t *testing.T) {
buf := bytes.NewBuffer([]byte{})
gob.NewEncoder(buf).Encode(uint64(1))
seqnPart := buf.String()
buf = bytes.NewBuffer([]byte{})
gob.NewEncoder(buf).Encode(emptyDir)
valPart := buf.String()
valPart = valPart[0 : len(valPart)/2]
st := New()
st.Ops <- Op{1, seqnPart + valPart}
st.Sync(1)
// check that we aren't leaking memory
assert.Equal(t, 0, len(st.todo))
}
示例13: serialize
// serialize encodes a session value using gob.
func serialize(session SessionData) ([]byte, error) {
b := bytes.NewBuffer(nil)
e := gob.NewEncoder(b)
if err := e.Encode(session); err != nil {
return nil, err
}
return b.Bytes(), nil
}
示例14: Save
func (Tgene *TGene) Save(file io.Writer) {
enc := gob.NewEncoder(file)
err := enc.Encode(Tgene)
if err != nil {
panic(err.String())
}
return
}
示例15: TestMe
func TestMe(t *testing.T) {
if len(Recipe_list) == 0 {
t.Error(":(")
}
e := gob.NewEncoder(os.Stdout)
//r := Recipe_list[0]
e.Encode(Recipe_list)
}