亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

directory search
archive archive/tar archive/zip bufio bufio(緩存) builtin builtin(內(nèi)置包) bytes bytes(包字節(jié)) compress compress/bzip2(壓縮/bzip2) compress/flate(壓縮/flate) compress/gzip(壓縮/gzip) compress/lzw(壓縮/lzw) compress/zlib(壓縮/zlib) container container/heap(容器數(shù)據(jù)結(jié)構(gòu)heap) container/list(容器數(shù)據(jù)結(jié)構(gòu)list) container/ring(容器數(shù)據(jù)結(jié)構(gòu)ring) context context(上下文) crypto crypto(加密) crypto/aes(加密/aes) crypto/cipher(加密/cipher) crypto/des(加密/des) crypto/dsa(加密/dsa) crypto/ecdsa(加密/ecdsa) crypto/elliptic(加密/elliptic) crypto/hmac(加密/hmac) crypto/md5(加密/md5) crypto/rand(加密/rand) crypto/rc4(加密/rc4) crypto/rsa(加密/rsa) crypto/sha1(加密/sha1) crypto/sha256(加密/sha256) crypto/sha512(加密/sha512) crypto/subtle(加密/subtle) crypto/tls(加密/tls) crypto/x509(加密/x509) crypto/x509/pkix(加密/x509/pkix) database database/sql(數(shù)據(jù)庫/sql) database/sql/driver(數(shù)據(jù)庫/sql/driver) debug debug/dwarf(調(diào)試/dwarf) debug/elf(調(diào)試/elf) debug/gosym(調(diào)試/gosym) debug/macho(調(diào)試/macho) debug/pe(調(diào)試/pe) debug/plan9obj(調(diào)試/plan9obj) encoding encoding(編碼) encoding/ascii85(編碼/ascii85) encoding/asn1(編碼/asn1) encoding/base32(編碼/base32) encoding/base64(編碼/base64) encoding/binary(編碼/binary) encoding/csv(編碼/csv) encoding/gob(編碼/gob) encoding/hex(編碼/hex) encoding/json(編碼/json) encoding/pem(編碼/pem) encoding/xml(編碼/xml) errors errors(錯誤) expvar expvar flag flag(命令行參數(shù)解析flag包) fmt fmt go go/ast(抽象語法樹) go/build go/constant(常量) go/doc(文檔) go/format(格式) go/importer go/parser go/printer go/scanner(掃描儀) go/token(令牌) go/types(類型) hash hash(散列) hash/adler32 hash/crc32 hash/crc64 hash/fnv html html html/template(模板) image image(圖像) image/color(顏色) image/color/palette(調(diào)色板) image/draw(繪圖) image/gif image/jpeg image/png index index/suffixarray io io io/ioutil log log log/syslog(日志系統(tǒng)) math math math/big math/big math/bits math/bits math/cmplx math/cmplx math/rand math/rand mime mime mime/multipart(多部分) mime/quotedprintable net net net/http net/http net/http/cgi net/http/cookiejar net/http/fcgi net/http/httptest net/http/httptrace net/http/httputil net/http/internal net/http/pprof net/mail net/mail net/rpc net/rpc net/rpc/jsonrpc net/smtp net/smtp net/textproto net/textproto net/url net/url os os os/exec os/signal os/user path path path/filepath(文件路徑) plugin plugin(插件) reflect reflect(反射) regexp regexp(正則表達式) regexp/syntax runtime runtime(運行時) runtime/debug(調(diào)試) runtime/internal/sys runtime/pprof runtime/race(競爭) runtime/trace(執(zhí)行追蹤器) sort sort(排序算法) strconv strconv(轉(zhuǎn)換) strings strings(字符串) sync sync(同步) sync/atomic(原子操作) syscall syscall(系統(tǒng)調(diào)用) testing testing(測試) testing/iotest testing/quick text text/scanner(掃描文本) text/tabwriter text/template(定義模板) text/template/parse time time(時間戳) unicode unicode unicode/utf16 unicode/utf8 unsafe unsafe
characters

  • import "encoding/binary"

  • Overview

  • Index

  • Examples

概觀

軟件包二進制實現(xiàn)了數(shù)字和字節(jié)序列之間的簡單轉(zhuǎn)換以及varint的編碼和解碼。

數(shù)字通過讀取和寫入固定大小的值進行翻譯。固定大小的值可以是固定大小的算術(shù)類型(bool,int8,uint8,int16,float32,complex64,...),也可以是只包含固定大小值的數(shù)組或結(jié)構(gòu)。

varint函數(shù)使用可變長度編碼對單個整數(shù)值進行編碼和解碼; 較小的值需要較少的字節(jié)。有關(guān)規(guī)范,請參閱https://developers.google.com/protocol-buffers/docs/encoding。

此包傾向于簡化而不是效率。需要高性能序列化的客戶端,尤其是大型數(shù)據(jù)結(jié)構(gòu)的客戶端,應(yīng)該考慮更高級的解決方案,如編碼/ gob包或協(xié)議緩沖區(qū)。

Index

  • Constants

  • Variables

  • func PutUvarint(buf []byte, x uint64) int

  • func PutVarint(buf []byte, x int64) int

  • func Read(r io.Reader, order ByteOrder, data interface{}) error

  • func ReadUvarint(r io.ByteReader) (uint64, error)

  • func ReadVarint(r io.ByteReader) (int64, error)

  • func Size(v interface{}) int

  • func Uvarint(buf []byte) (uint64, int)

  • func Varint(buf []byte) (int64, int)

  • func Write(w io.Writer, order ByteOrder, data interface{}) error

  • type ByteOrder

例子

ByteOrder(Get)ByteOrder(Put)PutUvarint PutVarint Read Uvarint Varint Write Write(Multi)

包文件

binary.go varint.go

常量

MaxVarintLenN是varint編碼的N位整數(shù)的最大長度。

const (
        MaxVarintLen16 = 3
        MaxVarintLen32 = 5
        MaxVarintLen64 = 10)

變量

BigEndian是ByteOrder的大端實現(xiàn)。

var BigEndian bigEndian

LittleEndian是ByteOrder的小端實現(xiàn)。

var LittleEndian littleEndian

func PutUvarint

func PutUvarint(buf []byte, x uint64) int

PutUvarint將uint64編碼到buf中,并返回寫入的字節(jié)數(shù)。如果緩沖區(qū)太小,PutUvarint會驚慌失措。

package mainimport ("encoding/binary""fmt")func main() {
	buf := make([]byte, binary.MaxVarintLen64)for _, x := range []uint64{1, 2, 127, 128, 255, 256} {
		n := binary.PutUvarint(buf, x)
		fmt.Printf("%x\n", buf[:n])}}

func PutVarint

func PutVarint(buf []byte, x int64) int

PutVarint將int64編碼為buf并返回寫入的字節(jié)數(shù)。如果緩沖區(qū)太小,PutVarint會驚慌失措。

package mainimport ("encoding/binary""fmt")func main() {
	buf := make([]byte, binary.MaxVarintLen64)for _, x := range []int64{-65, -64, -2, -1, 0, 1, 2, 63, 64} {
		n := binary.PutVarint(buf, x)
		fmt.Printf("%x\n", buf[:n])}}

func Read

func Read(r io.Reader, order ByteOrder, data interface{}) error

讀取從r讀取結(jié)構(gòu)化二進制數(shù)據(jù)到數(shù)據(jù)。數(shù)據(jù)必須是指向固定大小值或固定大小值的指針。從r讀取的字節(jié)使用指定的字節(jié)順序解碼并寫入數(shù)據(jù)的連續(xù)字段。當(dāng)解碼布爾值時,零字節(jié)被解碼為假,并且任何其他非零字節(jié)被解碼為真。在讀入結(jié)構(gòu)時,跳過帶空白(_)字段名稱的字段的字段數(shù)據(jù); 即空白字段名稱可用于填充。讀入結(jié)構(gòu)時,必須導(dǎo)出所有非空白字段或讀取可能會出現(xiàn)混亂。

只有在沒有字節(jié)被讀取的情況下,錯誤才是EOF。如果在讀取一些但不是全部字節(jié)后發(fā)生EOF,則Read返回ErrUnexpectedEOF。

package mainimport ("bytes""encoding/binary""fmt")func main() {var pi float64
	b := []byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40}
	buf := bytes.NewReader(b)
	err := binary.Read(buf, binary.LittleEndian, &pi)if err != nil {
		fmt.Println("binary.Read failed:", err)}
	fmt.Print(pi)}

func ReadUvarint

func ReadUvarint(r io.ByteReader) (uint64, error)

ReadUvarint從r讀取一個編碼的無符號整數(shù)并將其作為uint64返回。

func ReadVarint

func ReadVarint(r io.ByteReader) (int64, error)

ReadVarint從r中讀取一個已編碼的有符號整數(shù)并將其作為int64返回。

func Size

func Size(v interface{}) int

Size返回Write將生成的值,以便對值v進行編碼,該值必須是固定大小的值或固定大小的值片段或指向此類數(shù)據(jù)的指針。如果v不是這些,則Size返回-1。

func Uvarint

func Uvarint(buf []byte) (uint64, int)

Uvarint從buf解碼uint64并返回該值和讀取的字節(jié)數(shù)(> 0)。如果發(fā)生錯誤,則該值為0,并且字節(jié)數(shù)n <= 0意味著:

n == 0: buf too small
n  < 0: value larger than 64 bits (overflow)
        and -n is the number of bytes read

package mainimport ("encoding/binary""fmt")func main() {
	inputs := [][]byte{[]byte{0x01},[]byte{0x02},[]byte{0x7f},[]byte{0x80, 0x01},[]byte{0xff, 0x01},[]byte{0x80, 0x02},}for _, b := range inputs {
		x, n := binary.Uvarint(b)if n != len(b) {
			fmt.Println("Uvarint did not consume all of in")}
		fmt.Println(x)}}

func Varint

func Varint(buf []byte) (int64, int)

Varint從buf解碼int64并返回該值和讀取的字節(jié)數(shù)(> 0)。如果發(fā)生錯誤,則值為0,并且字節(jié)數(shù)n <= 0,其含義如下:

n == 0: buf too small
n  < 0: value larger than 64 bits (overflow)
        and -n is the number of bytes read

package mainimport ("encoding/binary""fmt")func main() {
	inputs := [][]byte{[]byte{0x81, 0x01},[]byte{0x7f},[]byte{0x03},[]byte{0x01},[]byte{0x00},[]byte{0x02},[]byte{0x04},[]byte{0x7e},[]byte{0x80, 0x01},}for _, b := range inputs {
		x, n := binary.Varint(b)if n != len(b) {
			fmt.Println("Varint did not consume all of in")}
		fmt.Println(x)}}

func Write

func Write(w io.Writer, order ByteOrder, data interface{}) error

寫將數(shù)據(jù)的二進制表示寫入w。數(shù)據(jù)必須是固定大小的值或固定大小的值片段,或指向這些數(shù)據(jù)的指針。布爾值編碼為一個字節(jié):1為真,0為假。寫入w的字節(jié)使用指定的字節(jié)順序進行編碼,并從數(shù)據(jù)的連續(xù)字段中讀取。在編寫結(jié)構(gòu)時,將為具有空白(_)字段名稱的字段寫入零值。

package mainimport ("bytes""encoding/binary""fmt""math")func main() {
	buf := new(bytes.Buffer)var pi float64 = math.Pi
	err := binary.Write(buf, binary.LittleEndian, pi)if err != nil {
		fmt.Println("binary.Write failed:", err)}
	fmt.Printf("% x", buf.Bytes())}

Example (Multi)

package mainimport ("bytes""encoding/binary""fmt")func main() {
	buf := new(bytes.Buffer)var data = []interface{}{uint16(61374),int8(-54),uint8(254),}for _, v := range data {
		err := binary.Write(buf, binary.LittleEndian, v)if err != nil {
			fmt.Println("binary.Write failed:", err)}}
	fmt.Printf("%x", buf.Bytes())}

type ByteOrder

ByteOrder指定如何將字節(jié)序列轉(zhuǎn)換為16,32或64位無符號整數(shù)。

type ByteOrder interface {        Uint16([]byte) uint16        Uint32([]byte) uint32        Uint64([]byte) uint64        PutUint16([]byte, uint16)        PutUint32([]byte, uint32)        PutUint64([]byte, uint64)        String() string}

Example (Get)

package mainimport ("encoding/binary""fmt")func main() {
	b := []byte{0xe8, 0x03, 0xd0, 0x07}
	x1 := binary.LittleEndian.Uint16(b[0:])
	x2 := binary.LittleEndian.Uint16(b[2:])
	fmt.Printf("%#04x %#04x\n", x1, x2)}

Example (Put)

package mainimport ("encoding/binary""fmt")func main() {
	b := make([]byte, 4)
	binary.LittleEndian.PutUint16(b[0:], 0x03e8)
	binary.LittleEndian.PutUint16(b[2:], 0x07d0)
	fmt.Printf("% x\n", b)}
Previous article: Next article: