golang實(shí)現(xiàn)java uuid的序列化方法
目前只實(shí)現(xiàn)了java生成的固定的uuid:85bb94b8-fd4b-4e1c-8f49-3cedd49d8f28的序列化
package main
import (
"encoding/binary"
"encoding/json"
"fmt"
"log"
"os"
"strings"
"time"
"github.com/Shopify/sarama"
"github.com/google/uuid"
)
const (
DATE_TIME_PATTERN = ""
STREAM_MAGIC = 0xaced
STREAM_VERSION = 5
TC_STRING = 0x74
TC_OBJECT = 0x73
TC_CLASSDESC = 0x72
SC_SERIALIZABLE = 0x02
TC_ENDBLOCKDATA = 0x78
TC_NULL = 0x70
)
func main() {
uuidTest()
}
func uuidTest() {
f, _ := os.Create("uuid-go.out")
defer f.Close()
f.Write(ShortBytes(STREAM_MAGIC))
f.Write(ShortBytes(STREAM_VERSION))
f.Write([]byte{TC_OBJECT})
f.Write([]byte{TC_CLASSDESC})
className := "java.util.UUID"
classNameLen := len(className)
f.Write(ShortBytes(uint16(classNameLen)))
f.Write([]byte(className))
sid := -4856846361193249489
f.Write(LongBytes(uint64(sid)))
//flags
f.Write([]byte{2})
//fields length
f.Write(ShortBytes(2))
//field type code
f.Write([]byte{'J'})
f1 := "leastSigBits"
f1Len := len(f1)
f.Write(ShortBytes(uint16(f1Len)))
f.Write([]byte(f1))
//filed type code
f.Write([]byte{'J'})
f2 := "mostSigBits"
f2Len := len(f2)
f.Write(ShortBytes(uint16(f2Len)))
f.Write([]byte(f2))
f.Write([]byte{TC_ENDBLOCKDATA})
f.Write([]byte{TC_NULL})
leastSigBits := -8121893460813967576
f.Write(LongBytes(uint64(leastSigBits)))
mostSigBits := -8810284723775779300
f.Write(LongBytes(uint64(mostSigBits)))
}
func ShortBytes(i uint16) []byte {
bytes := make([]byte, 2)
binary.BigEndian.PutUint16(bytes, i)
return bytes
}
func LongBytes(i uint64) []byte {
bytes := make([]byte, 8)
binary.BigEndian.PutUint64(bytes, i)
return bytes
}
func BigEndian() { // 大端序
// 二進(jìn)制形式:0000 0000 0000 0000 0001 0002 0003 0004
var testInt int32 = 0x01020304 // 十六進(jìn)制表示
fmt.Printf("%d use big endian: \n", testInt)
var testBytes []byte = make([]byte, 4)
binary.BigEndian.PutUint32(testBytes, uint32(testInt)) //大端序模式
fmt.Println("int32 to bytes:", testBytes)
convInt := binary.BigEndian.Uint32(testBytes) //大端序模式的字節(jié)轉(zhuǎn)為int32
fmt.Printf("bytes to int32: %d\n\n", convInt)
}
func LittleEndian() { // 小端序
//二進(jìn)制形式: 0000 0000 0000 0000 0001 0002 0003 0004
var testInt int32 = 0x01020304 // 16進(jìn)制
fmt.Printf("%d use little endian: \n", testInt)
var testBytes []byte = make([]byte, 4)
binary.LittleEndian.PutUint32(testBytes, uint32(testInt)) //小端序模式
fmt.Println("int32 to bytes:", testBytes)
convInt := binary.LittleEndian.Uint32(testBytes) //小端序模式的字節(jié)轉(zhuǎn)換
fmt.Printf("bytes to int32: %d\n\n", convInt)
}
func Int64ToBytes(i int64) []byte {
var buf = make([]byte, 8)
binary.BigEndian.PutUint64(buf, uint64(i))
return buf
}
java讀取測試
public class Test {
public static void main(String[] args) throws IOException, ClassNotFoundException {
readUUIDTest();
}
private static void readUUIDTest() throws IOException, ClassNotFoundException {
try (var fis = new FileInputStream("uuid-go.out"); var is = new ObjectInputStream(fis)) {
var uuid = is.readObject();
System.out.print(uuid);
}
}
}
到此這篇關(guān)于golang實(shí)現(xiàn)java uuid的序列化方法的文章就介紹到這了,更多相關(guān)golang實(shí)現(xiàn)java uuid序列化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Go實(shí)現(xiàn)快速生成固定長度的隨機(jī)字符串
這篇文章主要為大家詳細(xì)介紹了怎樣在Go中簡單快速地生成固定長度的隨機(jī)字符串,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,需要的可以學(xué)習(xí)一下2022-10-10
Go?Web實(shí)戰(zhàn)之創(chuàng)建項(xiàng)目及增加日志功能
這篇文章主要為大家詳細(xì)介紹了Go?Web項(xiàng)目中如何實(shí)現(xiàn)創(chuàng)建項(xiàng)目及增加日志功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-11-11
在go文件服務(wù)器加入http.StripPrefix的用途介紹
這篇文章主要介紹了在go文件服務(wù)器加入http.StripPrefix的用途介紹,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12
通過Go channel批量讀取數(shù)據(jù)的示例詳解
批量處理的主要邏輯是:從 channel 中接收數(shù)據(jù),積累到一定數(shù)量或者達(dá)到時間限制后,將數(shù)據(jù)批量處理(例如發(fā)送到 Kafka 或者寫入網(wǎng)絡(luò)),下面我將展示一個從 Go channel 中批量讀取數(shù)據(jù),并批量發(fā)送到 Kafka 和批量寫入網(wǎng)絡(luò)數(shù)據(jù)的示例,需要的朋友可以參考下2024-10-10
Go?iota關(guān)鍵字與枚舉類型實(shí)現(xiàn)原理
這篇文章主要介紹了Go?iota關(guān)鍵字與枚舉類型實(shí)現(xiàn)原理,iota是go語言的常量計(jì)數(shù)器,只能在常量的表達(dá)式中使用,更多相關(guān)內(nèi)容需要的小伙伴可以參考一下2022-07-07
詳解golang避免循環(huán)import問題(“import cycle not allowed”)
這篇文章主要給大家介紹了關(guān)于golang中不允許循環(huán)import問題("import cycle not allowed")的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08

