本文整理汇总了Golang中gob.NewEncoder函数的典型用法代码示例。如果您正苦于以下问题:Golang NewEncoder函数的具体用法?Golang NewEncoder怎么用?Golang NewEncoder使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewEncoder函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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)
}
开发者ID:andradeandrey,项目名称:doozer,代码行数:15,代码来源:node_test.go
示例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
}
开发者ID:vanackere,项目名称:replican-sync,代码行数:25,代码来源:gob.go
示例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
}
开发者ID:ashokgelal,项目名称:gorilla,代码行数:7,代码来源:key.go
示例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
}
开发者ID:fedgrant,项目名称:tonika,代码行数:33,代码来源:msg.go
示例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())
}
}
开发者ID:pcgod,项目名称:grumble,代码行数:26,代码来源:server.go
示例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
}
开发者ID:qtse,项目名称:go_fetch,代码行数:31,代码来源:persist.go
示例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
}
开发者ID:ssrl,项目名称:gosqlite3,代码行数:30,代码来源:query_parameter.go
示例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")
}
开发者ID:ipeet,项目名称:camlistore,代码行数:25,代码来源:flatcache.go
示例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())
}
开发者ID:sim82,项目名称:go_tools,代码行数:26,代码来源:tree_parser.go
示例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
}
开发者ID:sarnowski,项目名称:google-go-lang-idea-plugin,代码行数:7,代码来源:memcache.go
示例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)
}
}
}
}
开发者ID:Sunmonds,项目名称:gcc,代码行数:31,代码来源:rat_test.go
示例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))
}
开发者ID:andradeandrey,项目名称:doozer,代码行数:17,代码来源:store_test.go
示例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
}
开发者ID:klinster,项目名称:Bessie,代码行数:9,代码来源:sessions.go
示例14: Save
func (Tgene *TGene) Save(file io.Writer) {
enc := gob.NewEncoder(file)
err := enc.Encode(Tgene)
if err != nil {
panic(err.String())
}
return
}
开发者ID:ericfode,项目名称:Datazome,代码行数:8,代码来源:tGene.go
示例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)
}
开发者ID:newblue,项目名称:recipe,代码行数:8,代码来源:recipe_test.go
示例16: Call
// Call invokes a delayed function.
// Note that the function will be executed later.
func (f *Function) Call(c appengine.Context, args ...interface{}) {
if f.err != nil {
c.Errorf("delay: func is invalid: %v", f.err)
return
}
nArgs := len(args) + 1 // +1 for the appengine.Context
ft := f.fv.Type()
minArgs := ft.NumIn()
if ft.IsVariadic() {
minArgs--
}
if nArgs < minArgs {
c.Errorf("delay: too few arguments to func: %d < %d", nArgs, minArgs)
return
}
if !ft.IsVariadic() && nArgs > minArgs {
c.Errorf("delay: too many arguments to func: %d > %d", nArgs, minArgs)
return
}
// Check arg types.
for i := 1; i < nArgs; i++ {
at := reflect.TypeOf(args[i-1])
var dt reflect.Type
if i < minArgs {
// not a variadic arg
dt = ft.In(i)
} else {
// a variadic arg
dt = ft.In(minArgs).Elem()
}
if !at.AssignableTo(dt) {
c.Errorf("delay: argument %d has wrong type: %v is not assignable to %v", i, at, dt)
return
}
}
inv := invocation{
Key: f.key,
Args: args,
}
buf := new(bytes.Buffer)
if err := gob.NewEncoder(buf).Encode(inv); err != nil {
c.Errorf("delay: gob encoding failed: %v", err)
return
}
task := &taskqueue.Task{
Path: path,
Payload: buf.Bytes(),
}
if _, err := taskqueueAdder(c, task, queue); err != nil {
c.Errorf("delay: taskqueue.Add failed: %v", err)
return
}
}
开发者ID:ashokgelal,项目名称:gorilla,代码行数:60,代码来源:delay.go
示例17: WriteGob
// Write the game database in a fast binary Go-specific format
func WriteGob(filename string, database *Database) {
f, err := os.Open(filename, os.O_WRONLY|os.O_TRUNC|os.O_CREAT, 0x1a4) // 0644
if err != nil {
panic(fmt.Sprintf("error saving %s: %s", filename, err))
}
e := gob.NewEncoder(f)
e.Encode(database)
}
开发者ID:shellreef,项目名称:gones,代码行数:10,代码来源:cartdb.go
示例18: putMetaInfo
func putMetaInfo(fin, fout *os.File, cb []cbRecord) {
var meta nMeta
meta.Cb = make([]cbRecord, 256)
for k, v := range cb {
meta.Cb[k] = v
}
meta.Fsize = GetFileSize(fin)
PanicIf(gob.NewEncoder(fout).Encode(meta))
}
开发者ID:kravitz,项目名称:moroz,代码行数:9,代码来源:huffman.go
示例19: newEncodeDecodeCloser
func newEncodeDecodeCloser(rwc io.ReadWriteCloser) *EncodeDecodeCloser {
p := prof.NewReadWriteCloser(rwc)
return &EncodeDecodeCloser{
p,
gob.NewEncoder(p),
gob.NewDecoder(p),
p,
}
}
开发者ID:fedgrant,项目名称:tonika,代码行数:9,代码来源:util.go
示例20: NewClient
// NewClient returns a new Client to handle requests to the
// set of services at the other end of the connection.
func NewClient(conn io.ReadWriteCloser) *Client {
client := new(Client);
client.conn = conn;
client.enc = gob.NewEncoder(conn);
client.dec = gob.NewDecoder(conn);
client.pending = make(map[uint64]*Call);
go client.input();
return client;
}
开发者ID:8l,项目名称:go-learn,代码行数:11,代码来源:client.go
注:本文中的gob.NewEncoder函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论