• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang binary.PutVarint函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中encoding/binary.PutVarint函数的典型用法代码示例。如果您正苦于以下问题:Golang PutVarint函数的具体用法?Golang PutVarint怎么用?Golang PutVarint使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了PutVarint函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: Marshal

func (t *LeaseMetadata) Marshal(wire io.Writer) {
	var b [10]byte
	var bs []byte
	bs = b[:]
	alen1 := int64(len(t.Quorum))
	if wlen := binary.PutVarint(bs, alen1); wlen >= 0 {
		wire.Write(b[0:wlen])
	}
	for i := int64(0); i < alen1; i++ {
		bs = b[:4]
		tmp32 := t.Quorum[i]
		bs[0] = byte(tmp32)
		bs[1] = byte(tmp32 >> 8)
		bs[2] = byte(tmp32 >> 16)
		bs[3] = byte(tmp32 >> 24)
		wire.Write(bs)
	}
	bs = b[:]
	alen2 := int64(len(t.ObjectKeys))
	if wlen := binary.PutVarint(bs, alen2); wlen >= 0 {
		wire.Write(b[0:wlen])
	}
	for i := int64(0); i < alen2; i++ {
		t.ObjectKeys[i].Marshal(wire)
	}
	bs = b[:2]
	bs[0] = byte(t.IgnoreReplicas)
	bs[1] = byte(t.ReinstateReplicas)
	wire.Write(bs)
}
开发者ID:pombredanne,项目名称:qlease,代码行数:30,代码来源:qlprotomarsh.go


示例2: Increment

func (b *BoltDB) Increment() int64 {
	buf := make([]byte, 8)
	var retVal int64
	err := b.db.Update(func(tx *bolt.Tx) error {
		b, err := tx.CreateBucketIfNotExists(demoBucket)
		if err != nil {
			log.Fatal(err)
		}
		v := b.Get(demoKey)
		if v == nil {
			binary.PutVarint(buf, 0)
			retVal = 0
		} else {
			i, err := binary.ReadVarint(bytes.NewBuffer(v))
			if err != nil {
				log.Fatal(err)
			}
			i++
			retVal = i
			binary.PutVarint(buf, i)
		}
		err = b.Put(demoKey, buf)
		return err
	})
	if err != nil {
		log.Fatal(err)
	}

	return retVal
}
开发者ID:TrevorSStone,项目名称:boltmobiledemo,代码行数:30,代码来源:boltmobiledemo.go


示例3: writeNumber

func (b *binaryNomsWriter) writeNumber(v Number) {
	b.ensureCapacity(binary.MaxVarintLen64 * 2)
	i, exp := float64ToIntExp(float64(v))
	count := binary.PutVarint(b.buff[b.offset:], i)
	b.offset += uint32(count)
	count = binary.PutVarint(b.buff[b.offset:], int64(exp))
	b.offset += uint32(count)
}
开发者ID:Richardphp,项目名称:noms,代码行数:8,代码来源:codec.go


示例4: zoneFilename

func zoneFilename(x, y int64, z int8) string {
	var buf [binary.MaxVarintLen64*2 + 1]byte
	i := binary.PutVarint(buf[:], x)
	i += binary.PutVarint(buf[i:], y)
	buf[i] = uint8(z)
	i++
	encoded := base32.StdEncoding.EncodeToString(buf[:i])
	return filepath.Join("rnoadm-AA", "zone"+encoded+".gz")
}
开发者ID:BenLubar,项目名称:Rnoadm,代码行数:9,代码来源:storage.go


示例5: putFunc

func putFunc(cmd *cobra.Command, args []string) {
	if keySpaceSize <= 0 {
		fmt.Fprintf(os.Stderr, "expected positive --key-space-size, got (%v)", keySpaceSize)
		os.Exit(1)
	}

	requests := make(chan v3.Op, totalClients)
	clients := mustCreateClients(totalClients, totalConns)
	k, v := make([]byte, keySize), string(mustRandBytes(valSize))

	bar = pb.New(putTotal)
	bar.Format("Bom !")
	bar.Start()

	r := newReport()
	for i := range clients {
		wg.Add(1)
		go func(c *v3.Client) {
			defer wg.Done()
			for op := range requests {
				st := time.Now()
				_, err := c.Do(context.Background(), op)
				r.Results() <- report.Result{Err: err, Start: st, End: time.Now()}
				bar.Increment()
			}
		}(clients[i])
	}

	go func() {
		for i := 0; i < putTotal; i++ {
			if seqKeys {
				binary.PutVarint(k, int64(i%keySpaceSize))
			} else {
				binary.PutVarint(k, int64(rand.Intn(keySpaceSize)))
			}
			requests <- v3.OpPut(string(k), v)
		}
		close(requests)
	}()

	if compactInterval > 0 {
		go func() {
			for {
				time.Sleep(compactInterval)
				compactKV(clients)
			}
		}()
	}

	rc := r.Run()
	wg.Wait()
	close(r.Results())
	bar.Finish()
	fmt.Println(<-rc)
}
开发者ID:pulcy,项目名称:vault-monkey,代码行数:55,代码来源:put.go


示例6: putFunc

func putFunc(cmd *cobra.Command, args []string) {
	if keySpaceSize <= 0 {
		fmt.Fprintf(os.Stderr, "expected positive --key-space-size, got (%v)", keySpaceSize)
		os.Exit(1)
	}

	results = make(chan result)
	requests := make(chan v3.Op, totalClients)
	bar = pb.New(putTotal)

	k, v := make([]byte, keySize), string(mustRandBytes(valSize))

	clients := mustCreateClients(totalClients, totalConns)

	bar.Format("Bom !")
	bar.Start()

	for i := range clients {
		wg.Add(1)
		go doPut(context.Background(), clients[i], requests)
	}

	pdoneC := printReport(results)

	go func() {
		for i := 0; i < putTotal; i++ {
			if seqKeys {
				binary.PutVarint(k, int64(i%keySpaceSize))
			} else {
				binary.PutVarint(k, int64(rand.Intn(keySpaceSize)))
			}
			requests <- v3.OpPut(string(k), v)
		}
		close(requests)
	}()

	if compactInterval > 0 {
		go func() {
			for {
				time.Sleep(compactInterval)
				compactKV(clients)
			}
		}()
	}

	wg.Wait()

	bar.Finish()

	close(results)
	<-pdoneC
}
开发者ID:CliffYuan,项目名称:etcd,代码行数:52,代码来源:put.go


示例7: zoneFilenameV1

func zoneFilenameV1(x, y int64) string {
	var buf [binary.MaxVarintLen64 * 2]byte
	i := binary.PutVarint(buf[:], x)
	i += binary.PutVarint(buf[i:], y)
	encoded := base32.StdEncoding.EncodeToString(buf[:i])
	for i := range encoded {
		if encoded[i] == '=' {
			encoded = encoded[:i]
			break
		}
	}
	return filepath.Join("rnoadm-AA", "zone"+encoded+".gz")
}
开发者ID:BenLubar,项目名称:Rnoadm,代码行数:13,代码来源:storage.go


示例8: putFunc

func putFunc(cmd *cobra.Command, args []string) {
	if keySpaceSize <= 0 {
		fmt.Fprintf(os.Stderr, "expected positive --key-space-size, got (%v)", keySpaceSize)
		os.Exit(1)
	}

	results = make(chan result)
	requests := make(chan etcdserverpb.PutRequest, totalClients)
	bar = pb.New(putTotal)

	k, v := make([]byte, keySize), mustRandBytes(valSize)

	conns := make([]*grpc.ClientConn, totalConns)
	for i := range conns {
		conns[i] = mustCreateConn()
	}

	clients := make([]etcdserverpb.KVClient, totalClients)
	for i := range clients {
		clients[i] = etcdserverpb.NewKVClient(conns[i%int(totalConns)])
	}

	bar.Format("Bom !")
	bar.Start()

	for i := range clients {
		wg.Add(1)
		go doPut(context.Background(), clients[i], requests)
	}

	pdoneC := printReport(results)

	go func() {
		for i := 0; i < putTotal; i++ {
			if seqKeys {
				binary.PutVarint(k, int64(i%keySpaceSize))
			} else {
				binary.PutVarint(k, int64(rand.Intn(keySpaceSize)))
			}
			requests <- etcdserverpb.PutRequest{Key: k, Value: v}
		}
		close(requests)
	}()

	wg.Wait()

	bar.Finish()

	close(results)
	<-pdoneC
}
开发者ID:ikatson,项目名称:etcd,代码行数:51,代码来源:put.go


示例9: Encode

func (e *CacheEntry) Encode() []byte {
	hl := 0
	for key, vals := range e.Header {
		hl = hl + binary.MaxVarintLen32*2 + len(key)
		for _, v := range vals {
			hl += binary.MaxVarintLen32 + len(v)
		}
	}

	size := 5*binary.MaxVarintLen32 + len(e.Body) + hl
	buffer := make([]byte, size)

	n := 0

	n += binary.PutVarint(buffer[n:], int64(e.Status))
	n += binary.PutVarint(buffer[n:], int64(e.AddTime))
	n += binary.PutVarint(buffer[n:], int64(e.Expire))
	n += binary.PutVarint(buffer[n:], int64(len(e.Body)))
	n += copy(buffer[n:], e.Body)
	n += binary.PutVarint(buffer[n:], int64(len(e.Header)))

	for key, vals := range e.Header {
		n += binary.PutVarint(buffer[n:], int64(len(key)))
		n += copy(buffer[n:], key)
		n += binary.PutVarint(buffer[n:], int64(len(vals)))
		for _, v := range vals {
			n += binary.PutVarint(buffer[n:], int64(len(v)))
			n += copy(buffer[n:], v)
		}
	}

	return buffer[:n]
}
开发者ID:shenfeng,项目名称:proxies,代码行数:33,代码来源:cache.go


示例10: MarshalDeltaNodes

func MarshalDeltaNodes(nodes []element.Node, buf []byte) []byte {
	estimatedLength := len(nodes)*4*3 + binary.MaxVarintLen64

	if cap(buf) < estimatedLength {
		buf = make([]byte, estimatedLength)
	} else {
		// resize slice to full capacity
		buf = buf[:cap(buf)-1]
	}

	lastId := int64(0)
	nextPos := binary.PutUvarint(buf, uint64(len(nodes)))

	for i := range nodes {
		if len(buf)-nextPos < binary.MaxVarintLen64 {
			tmp := make([]byte, len(buf)*3/2)
			copy(tmp, buf)
			buf = tmp
		}
		nextPos += binary.PutVarint(buf[nextPos:], nodes[i].Id-lastId)
		lastId = nodes[i].Id
	}

	lastLong := int64(0)
	for i := range nodes {
		if len(buf)-nextPos < binary.MaxVarintLen64 {
			tmp := make([]byte, len(buf)*3/2)
			copy(tmp, buf)
			buf = tmp
		}
		long := int64(CoordToInt(nodes[i].Long))
		nextPos += binary.PutVarint(buf[nextPos:], long-lastLong)
		lastLong = long
	}

	lastLat := int64(0)
	for i := range nodes {
		if len(buf)-nextPos < binary.MaxVarintLen64 {
			tmp := make([]byte, len(buf)*3/2)
			copy(tmp, buf)
			buf = tmp
		}
		lat := int64(CoordToInt(nodes[i].Lat))
		nextPos += binary.PutVarint(buf[nextPos:], lat-lastLat)
		lastLat = lat
	}

	return buf[:nextPos]
}
开发者ID:Kotaimen,项目名称:imposm3,代码行数:49,代码来源:deltacoords.go


示例11: TestIntSliceEncode

func TestIntSliceEncode(t *testing.T) {
	var result []byte
	result = strconv.AppendInt(result, 124, 10)
	if string(result) != "124" {
		t.Errorf("%v\n", result)
	}
	result = strconv.AppendInt(result, 12, 10)

	another := make([]byte, 14)
	nul := binary.PutVarint(another, 2)
	nul = binary.PutVarint(another[4:], 111111111)
	if nul < 0 {
		t.Error()
	}
}
开发者ID:compasses,项目名称:GOProjects,代码行数:15,代码来源:repo_test.go


示例12: growBucket

// Create and chain a new bucket.
func (ht *HashTable) growBucket(bucket int) {
	ht.EnsureSize(BUCKET_SIZE)
	lastBucketAddr := ht.lastBucket(bucket) * BUCKET_SIZE
	binary.PutVarint(ht.Buf[lastBucketAddr:lastBucketAddr+10], int64(ht.numBuckets))
	ht.Used += BUCKET_SIZE
	ht.numBuckets++
}
开发者ID:Lanzafame,项目名称:tiedot,代码行数:8,代码来源:hashtable.go


示例13: handleWrite

func handleWrite(c net.Conn) {
	for angle := range out {
		m := Message{angle}
		if buf, err := json.Marshal(m); err != nil {
			log.Printf("Could not json encode angle: %+v\n", m)
		} else {
			var frame []byte
			mode := byte(0x81)
			mask := byte(0x80)
			if len(buf) <= 125 {
				// can fit in the mask
				frame = []byte{mode, mask | byte(len(buf))}
			} else if len(buf) > 0xffff {
				// needs 8 extra bytes
				frame = make([]byte, 10)
				frame[0] = mode
				frame[1] = mask | 127
				// for now, len is 32-bit
				// TODO: update when 64-bit is supported
				// this should never happen anyway
				binary.PutVarint(frame[2:], int64(len(buf)))
			} else {
				// needs 2 extra bytes
				frame = []byte{
					mode,
					mask | 128,
					byte(len(buf)&0xff00) >> 8,
					byte(len(buf) & 0xff)}
			}

			c.Write(append(frame, buf...))
		}
	}
}
开发者ID:AungWinnHtut,项目名称:arduino-tcp-server,代码行数:34,代码来源:client.go


示例14: hashVarint

func (rv *rollingValueHasher) hashVarint(n int64) {
	buff := [binary.MaxVarintLen64]byte{}
	count := binary.PutVarint(buff[:], n)
	for i := 0; i < count; i++ {
		rv.HashByte(buff[i])
	}
}
开发者ID:kalman,项目名称:noms-pre-release,代码行数:7,代码来源:rolling_value_hasher.go


示例15: VerifySpace

func (v *Verifier) VerifySpace(challenges []int64, hashes [][]byte, parents [][][]byte, proofs [][][]byte, pProofs [][][][]byte) bool {
	for i := range challenges {
		buf := make([]byte, hashSize)
		binary.PutVarint(buf, challenges[i]+v.pow2)
		val := append(v.pk, buf...)
		for _, ph := range parents[i] {
			val = append(val, ph...)
		}
		exp := sha3.Sum256(val)
		for j := range exp {
			if exp[j] != hashes[i][j] {
				return false
			}
		}
		if !v.Verify(challenges[i], hashes[i], proofs[i]) {
			return false
		}

		ps := v.graph.GetParents(challenges[i], v.index)
		for j := range ps {
			if !v.Verify(ps[j], parents[i][j], pProofs[i][j]) {
				return false
			}
		}
	}
	return true
}
开发者ID:kwonalbert,项目名称:spacemint,代码行数:27,代码来源:verifier.go


示例16: IntToBytes

func IntToBytes(v int64) []byte {
	buf := make([]byte, indexOffsetSize)

	binary.PutVarint(buf, v)

	return buf
}
开发者ID:joerocklin,项目名称:fastpack,代码行数:7,代码来源:struct_fileindex.go


示例17: Sign

func (s *Signer) Sign(msg []byte) []byte {
	b := make([]byte, 8+len(msg))
	binary.PutVarint(b, int64(len(msg)))
	b = append(b[:8], msg...)
	b = append(b, s.signature(b)...)
	return b
}
开发者ID:jmptrader,项目名称:signer,代码行数:7,代码来源:signer.go


示例18: GetEntropyMixed

// TODO: verify. this needs to be audited
// we start with crypt/rand, then XOR in additional entropy from OS
func GetEntropyMixed(n int) []byte {
	startTime := time.Now().UnixNano()
	// for each source, we take SHA3 of the source and use it as seed to math/rand
	// then read bytes from it and XOR them onto the bytes read from crypto/rand
	mainBuff := GetEntropyCSPRNG(n)
	// 1. OS entropy sources
	startTimeBytes := make([]byte, 32)
	binary.PutVarint(startTimeBytes, startTime)
	startTimeHash := Sha3(startTimeBytes)
	mixBytes(mainBuff, startTimeHash)

	pid := os.Getpid()
	pidBytes := make([]byte, 32)
	binary.PutUvarint(pidBytes, uint64(pid))
	pidHash := Sha3(pidBytes)
	mixBytes(mainBuff, pidHash)

	osEnv := os.Environ()
	osEnvBytes := []byte(strings.Join(osEnv, ""))
	osEnvHash := Sha3(osEnvBytes)
	mixBytes(mainBuff, osEnvHash)

	// not all OS have hostname in env variables
	osHostName, err := os.Hostname()
	if err != nil {
		osHostNameBytes := []byte(osHostName)
		osHostNameHash := Sha3(osHostNameBytes)
		mixBytes(mainBuff, osHostNameHash)
	}
	return mainBuff
}
开发者ID:alist,项目名称:mint-client,代码行数:33,代码来源:rand_entropy.go


示例19: UUID

// UUID returns a global unique identifier for the given literal. It is
// implemented as the SHA1 UUID of the literal value.
func (l *Literal) UUID() uuid.UUID {
	var buffer bytes.Buffer

	switch v := l.v.(type) {
	case bool:
		if v {
			buffer.WriteString("true")
		} else {
			buffer.WriteString("false")
		}
	case int64:
		b := make([]byte, 8)
		binary.PutVarint(b, v)
		buffer.Write(b)
	case float64:
		bs := math.Float64bits(v)
		b := make([]byte, 8)
		binary.LittleEndian.PutUint64(b, bs)
		buffer.Write(b)
	case string:
		buffer.Write([]byte(v))
	case []byte:
		buffer.Write(v)
	}

	return uuid.NewSHA1(uuid.NIL, buffer.Bytes())
}
开发者ID:google,项目名称:badwolf,代码行数:29,代码来源:literal.go


示例20: SetVersion

func (c *Cache) SetVersion(version int) error {
	return c.db.Update(func(tx *bolt.Tx) error {
		buf := make([]byte, 10)
		n := binary.PutVarint(buf, int64(version))
		return tx.Bucket(geoMetaBucket).Put([]byte("version"), buf[:n])
	})
}
开发者ID:pmezard,项目名称:apec,代码行数:7,代码来源:geocoder.go



注:本文中的encoding/binary.PutVarint函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang binary.Read函数代码示例发布时间:2022-05-24
下一篇:
Golang binary.PutUvarint函数代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap