Golang IntToByte 实现

package test01

import (
   "bytes"
   "encoding/binary"
   "fmt"
   "os"
)

func IntToByte(num int64) []byte {
   var buffer bytes.Buffer
   err := binary.Write(&buffer, binary.BigEndian, num)
   CheckErr(err)
   return buffer.Bytes()
}

func CheckErr(err error) {
   if err != nil {
      fmt.Println("err occur: ", err)
      os.Exit(1)
   }
}
// Write writes the binary representation of data into w.
// Data must be a fixed-size value or a slice of fixed-size
// values, or a pointer to such data.
// Bytes written to w are encoded using the specified byte order
// and read from successive fields of the data.
// When writing structs, zero values are written for fields
// with blank (_) field names.
func Write(w io.Writer, order ByteOrder, data interface{}) error {}

猜你喜欢

转载自blog.csdn.net/zhichunqi/article/details/80875290