go转换demo
//整形转换成字节
func IntToBytes(val int64) ([]byte, error) {
bytesBuffer := bytes.NewBuffer([]byte{})
if err := binary.Write(bytesBuffer, binary.BigEndian, val); err != nil {
return nil, err
}
return bytesBuffer.Bytes(), nil
}
//字节转换成整形
func BytesToInt(b []byte) (int64, error) {
bytesBuffer := bytes.NewBuffer(b)
var iVal int64
if err := binary.Read(bytesBuffer, binary.BigEndian, &iVal); err != nil {
return -1, err
}
return iVal, nil
}
func TestConv(t *testing.T) {
millTime := time.Now().UnixNano() / 1e6
fmt.Println("time1:", millTime)
b, _ := IntToBytes(millTime)
iTime, _ := BytesToInt(b)
fmt.Println("time2:", iTime)
}
关注微信公众号,了解更多golang及后端开发相关知识