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


Golang binary.ReadVarint函數代碼示例

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


在下文中一共展示了ReadVarint函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: readVarint

func (bin *mBinaryProto) readVarint(reader io.Reader) (int64, error) {
	if br, ok := reader.(io.ByteReader); ok {
		return binary.ReadVarint(br)
	}
	br := newByteReader(reader, bin.readBuffer)
	return binary.ReadVarint(br)
}
開發者ID:msglib,項目名稱:msglib-go,代碼行數:7,代碼來源:msglib.go

示例2: SaveRow

//  0. embedded video
//  1. picture
//  2. pictures
//  3. title
//  4. tags
//  5. categories
//  6. pornstars
//  7. duration in seconds
//  8. views
//  9. upvotes
//  10. downvotes
func SaveRow(row []byte) {
	var video Video

	// Split the columns
	b := bytes.Split(row, []byte(`|`))

	video.SourceName = "pornhub"
	video.Username = "Pornhub"
	// video.EmbedCode = EscapeStrings(string(b[0]))
	video.EmbedCode = EscapeString(string(b[0]))
	video.Picture = string(b[1])
	video.Pictures = string(b[2])
	video.Title = string(b[3])
	video.Description = "Watch " + string(b[3])
	video.Tags = string(b[4])
	video.Categories = string(b[5])
	video.Pornstars = string(b[6])
	video.Duration, _ = binary.ReadVarint(bytes.NewBuffer(b[7]))
	video.ViewsCount, _ = binary.ReadVarint(bytes.NewBuffer(b[8]))
	video.Upvotes, _ = binary.ReadVarint(bytes.NewBuffer(b[9]))
	video.Downvotes, _ = binary.ReadVarint(bytes.NewBuffer(b[10]))
	video.CreatedAt = time.Now()
	video.UpdatedAt = time.Now()

	insertVideo(video)
}
開發者ID:shavit,項目名稱:Kese-Dollz,代碼行數:37,代碼來源:load_videos_line_by_line.go

示例3: UnmarshalBinary

// UnmarshalBinary implements encoding.BinaryUnmarshaler.
func (tr *TimeRange) UnmarshalBinary(buf []byte) error {
	r := bytes.NewReader(buf)
	first, err := binary.ReadVarint(r)
	if err != nil {
		return err
	}
	last, err := binary.ReadVarint(r)
	if err != nil {
		return err
	}
	tr.First = clientmodel.Timestamp(first)
	tr.Last = clientmodel.Timestamp(last)
	return nil
}
開發者ID:gitlabuser,項目名稱:prometheus,代碼行數:15,代碼來源:codable.go

示例4: UnmarshalDeltaNodes

func UnmarshalDeltaNodes(buf []byte, nodes []element.Node) ([]element.Node, error) {
	r := bytes.NewBuffer(buf)
	length, err := binary.ReadVarint(r)
	if err != nil {
		return nil, err
	}

	if int64(cap(nodes)) < length {
		nodes = make([]element.Node, length)
	} else {
		nodes = nodes[:length]
	}

	lastId := int64(0)
	for i := 0; int64(i) < length; i++ {
		id, err := binary.ReadVarint(r)
		if err != nil {
			return nil, err
		}
		id = lastId + id
		nodes[i].Id = id
		lastId = id
	}

	lastLong := int64(0)
	for i := 0; int64(i) < length; i++ {
		long, err := binary.ReadVarint(r)
		if err != nil {
			return nil, err
		}
		long = lastLong + long
		nodes[i].Long = IntToCoord(uint32(long))
		lastLong = long
	}

	lastLat := int64(0)
	for i := 0; int64(i) < length; i++ {
		lat, err := binary.ReadVarint(r)
		if err != nil {
			return nil, err
		}
		lat = lastLat + lat
		nodes[i].Lat = IntToCoord(uint32(lat))
		lastLat = lat
	}

	return nodes, nil
}
開發者ID:renderless,項目名稱:imposm3,代碼行數:48,代碼來源:deltacoords.go

示例5: Unmarshal

func (t *Commit) Unmarshal(rr io.Reader) error {
	var wire byteReader
	var ok bool
	if wire, ok = rr.(byteReader); !ok {
		wire = bufio.NewReader(rr)
	}
	var b [24]byte
	var bs []byte
	bs = b[:12]
	if _, err := io.ReadAtLeast(wire, bs, 12); err != nil {
		return err
	}
	t.LeaderId = int32((uint32(bs[0]) | (uint32(bs[1]) << 8) | (uint32(bs[2]) << 16) | (uint32(bs[3]) << 24)))
	t.Replica = int32((uint32(bs[4]) | (uint32(bs[5]) << 8) | (uint32(bs[6]) << 16) | (uint32(bs[7]) << 24)))
	t.Instance = int32((uint32(bs[8]) | (uint32(bs[9]) << 8) | (uint32(bs[10]) << 16) | (uint32(bs[11]) << 24)))
	alen1, err := binary.ReadVarint(wire)
	if err != nil {
		return err
	}
	t.Command = make([]state.Command, alen1)
	for i := int64(0); i < alen1; i++ {
		t.Command[i].Unmarshal(wire)
	}
	bs = b[:24]
	if _, err := io.ReadAtLeast(wire, bs, 24); err != nil {
		return err
	}
	t.Seq = int32((uint32(bs[0]) | (uint32(bs[1]) << 8) | (uint32(bs[2]) << 16) | (uint32(bs[3]) << 24)))
	t.Deps[0] = int32((uint32(bs[4]) | (uint32(bs[5]) << 8) | (uint32(bs[6]) << 16) | (uint32(bs[7]) << 24)))
	t.Deps[1] = int32((uint32(bs[8]) | (uint32(bs[9]) << 8) | (uint32(bs[10]) << 16) | (uint32(bs[11]) << 24)))
	t.Deps[2] = int32((uint32(bs[12]) | (uint32(bs[13]) << 8) | (uint32(bs[14]) << 16) | (uint32(bs[15]) << 24)))
	t.Deps[3] = int32((uint32(bs[16]) | (uint32(bs[17]) << 8) | (uint32(bs[18]) << 16) | (uint32(bs[19]) << 24)))
	t.Deps[4] = int32((uint32(bs[20]) | (uint32(bs[21]) << 8) | (uint32(bs[22]) << 16) | (uint32(bs[23]) << 24)))
	return nil
}
開發者ID:WIZARD-CXY,項目名稱:golang-devops-stuff,代碼行數:35,代碼來源:epaxosprotomarsh.go

示例6: rawInt64

// rawInt64 should only be used by low-level decoders
func (p *importer) rawInt64() int64 {
	i, err := binary.ReadVarint(p)
	if err != nil {
		panic(fmt.Sprintf("read error: %v", err))
	}
	return i
}
開發者ID:CyCoreSystems,項目名稱:coreos-kubernetes,代碼行數:8,代碼來源:bimport.go

示例7: Unmarshal

func (t *Accept) Unmarshal(rr io.Reader) error {
	var wire byteReader
	var ok bool
	if wire, ok = rr.(byteReader); !ok {
		wire = bufio.NewReader(rr)
	}
	var b [12]byte
	var bs []byte
	bs = b[:12]
	if _, err := io.ReadAtLeast(wire, bs, 12); err != nil {
		return err
	}
	t.LeaderId = int32((uint32(bs[0]) | (uint32(bs[1]) << 8) | (uint32(bs[2]) << 16) | (uint32(bs[3]) << 24)))
	t.Instance = int32((uint32(bs[4]) | (uint32(bs[5]) << 8) | (uint32(bs[6]) << 16) | (uint32(bs[7]) << 24)))
	t.Ballot = int32((uint32(bs[8]) | (uint32(bs[9]) << 8) | (uint32(bs[10]) << 16) | (uint32(bs[11]) << 24)))
	alen1, err := binary.ReadVarint(wire)
	if err != nil {
		return err
	}
	t.Command = make([]state.Command, alen1)
	for i := int64(0); i < alen1; i++ {
		t.Command[i].Unmarshal(wire)
	}
	if _, err := io.ReadAtLeast(wire, bs, 12); err != nil {
		return err
	}
	t.LeaseInstance = int32((uint32(bs[0]) | (uint32(bs[1]) << 8) | (uint32(bs[2]) << 16) | (uint32(bs[3]) << 24)))
	t.OriginReplica = int32((uint32(bs[4]) | (uint32(bs[5]) << 8) | (uint32(bs[6]) << 16) | (uint32(bs[7]) << 24)))
	t.PropId = int32((uint32(bs[8]) | (uint32(bs[9]) << 8) | (uint32(bs[10]) << 16) | (uint32(bs[11]) << 24)))
	return nil
}
開發者ID:pombredanne,項目名稱:qlease,代碼行數:31,代碼來源:paxosprotomarsh.go

示例8: GetValue

func (b *BoltDB) GetValue() int64 {
	var retVal int64
	err := b.db.View(func(tx *bolt.Tx) error {
		b := tx.Bucket(demoBucket)
		if b == nil {
			retVal = -1
			return nil
		}
		v := b.Get(demoKey)
		if v == nil {

			retVal = 0
		} else {
			i, err := binary.ReadVarint(bytes.NewBuffer(v))
			if err != nil {
				return err
			}
			retVal = i
		}
		return nil
	})
	if err != nil {
		log.Fatal(err)
	}
	return retVal
}
開發者ID:TrevorSStone,項目名稱:boltmobiledemo,代碼行數:26,代碼來源:boltmobiledemo.go

示例9: DecodeOld

func DecodeOld(str string) (int64, error) {
	data, err := base64.StdEncoding.DecodeString(str)
	if err != nil {
		return 0, err
	}
	return binary.ReadVarint(bytes.NewReader(data))
}
開發者ID:orian,項目名稱:utils,代碼行數:7,代碼來源:ids.go

示例10: main

// Testing out how to hash some object
func main() {
	fmt.Println("Hello, playground")

	// sha := sha256.New()

	result := sha256.Sum256([]byte("foobar"))
	length := len(result)
	fmt.Printf("%d\n", length)
	fmt.Printf("%x\n", result)

	part0 := result[:16]
	fmt.Printf("%d\n", len(part0))
	fmt.Printf("%x\n", part0)

	// converting byte array to signed int
	buf := bytes.NewBuffer(part0)
	intPart0, _ := binary.ReadVarint(buf)
	// note: probably want unsigned int instead...

	fmt.Printf("%d\n\n\n", intPart0)

	part1 := result[16:]
	fmt.Printf("%d\n", len(part1))
	fmt.Printf("%x\n", part1)

	b := NewBloomFilterStringKeyed(32, 10)

	fmt.Println(b.byteCapacity)
	fmt.Println(b.numHashes)
	fmt.Println(b.bitHashTable)

	b.AddKey("foo")

}
開發者ID:kaschaeffer,項目名稱:bloomfilter,代碼行數:35,代碼來源:bloomfilter.go

示例11: Unmarshal

func (t *PrepareReply) Unmarshal(rr io.Reader) error {
	var wire byteReader
	var ok bool
	if wire, ok = rr.(byteReader); !ok {
		wire = bufio.NewReader(rr)
	}
	var b [10]byte
	var bs []byte
	bs = b[:9]
	if _, err := io.ReadAtLeast(wire, bs, 9); err != nil {
		return err
	}
	t.Balnum = int32((uint32(bs[0]) | (uint32(bs[1]) << 8) | (uint32(bs[2]) << 16) | (uint32(bs[3]) << 24)))
	t.OK = uint8(bs[4])
	t.Ballot = int32((uint32(bs[5]) | (uint32(bs[6]) << 8) | (uint32(bs[7]) << 16) | (uint32(bs[8]) << 24)))
	alen1, err := binary.ReadVarint(wire)
	if err != nil {
		return err
	}
	t.Cstruct = make([]int32, alen1)
	for i := int64(0); i < alen1; i++ {
		bs = b[:4]
		if _, err := io.ReadAtLeast(wire, bs, 4); err != nil {
			return err
		}
		t.Cstruct[i] = int32((uint32(bs[0]) | (uint32(bs[1]) << 8) | (uint32(bs[2]) << 16) | (uint32(bs[3]) << 24)))
	}
	return nil
}
開發者ID:WIZARD-CXY,項目名稱:golang-devops-stuff,代碼行數:29,代碼來源:gpaxosprotomarsh.go

示例12: Read

//The go routine here, keep waiting messages from connected client Blocking.
func (client *Client) Read(mp *MessagePasser) {
	for {
		//line, err := client.reader.ReadBytes('\xfe')
		length, err := binary.ReadVarint(client.reader)

		if err != nil {
			client.rethrowError(mp)
			mp.RemoveMapping(client.IP)
			mp.RemoveMapping(client.name)
			return
		}
		buffer := make([]byte, length)
		client.reader.Read(buffer)

		msg := new(Message)
		msg.Deserialize(buffer)

		err = msg.Deserialize(buffer)
		if err != nil {
			fmt.Println("err is " + err.Error())
		}
		client.addToClients(msg, mp.connections.clients)
		mp.Incoming <- msg // Since there is only one socket, directly put all the received
		// msgs into the global receiving channel (message queue)
	}
}
開發者ID:wcyz666,項目名稱:DS_TeamProject,代碼行數:27,代碼來源:MessagePasser.go

示例13: Decode

func (bed BinaryVarintEncoderDecoder) Decode(r io.Reader, n *big.Float) error {
	var isInteger int8
	var f float64
	var exponent int32
	n.SetUint64(0)

	if err := binary.Read(r, binary.BigEndian, &isInteger); err != nil {
		return err
	}

	if isInteger <= 0 {
		var x int64
		var err error
		if x, err = binary.ReadVarint(miniByteReader{r}); err != nil {
			return err
		}
		n.SetInt64(x)
		n.SetPrec(ENCODER_DECODER_PREC)
		return nil
	} else {
		if err := binary.Read(r, binary.BigEndian, &f); err != nil {
			return err
		}
		if err := binary.Read(r, binary.BigEndian, &exponent); err != nil {
			return err
		}
		bed.tmp.SetFloat64(f)
		bed.tmp.SetPrec(ENCODER_DECODER_PREC)
		n.SetMantExp(bed.tmp, int(exponent))
		return nil
	}
}
開發者ID:Richardphp,項目名稱:noms,代碼行數:32,代碼來源:binary-varint-encoder.go

示例14: init

func init() {
	// seed math.random from crypto.random
	seedBytes, _ := makeRandom(8)
	seedBuf := bytes.NewBuffer(seedBytes)
	n64, _ := binary.ReadVarint(seedBuf)
	mrand.Seed(n64)
}
開發者ID:rsrsps,項目名稱:redoctober,代碼行數:7,代碼來源:passvault.go

示例15: rawInt64

// rawInt64 should only be used by low-level decoders.
func (p *importer) rawInt64() int64 {
	i, err := binary.ReadVarint(p)
	if err != nil {
		formatErrorf("read error: %v", err)
	}
	return i
}
開發者ID:kuangchanglang,項目名稱:go,代碼行數:8,代碼來源:bimport.go


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