SoFunction
Updated on 2025-03-04

Golang implements serialization method of java uuid

Currently, only the fixed uuid generated by Java is implemented: 85bb94b8-fd4b-4e1c-8f49-3cedd49d8f28 serialization

package main

import (
  "encoding/binary"
  "encoding/json"
  "fmt"
  "log"
  "os"
  "strings"
  "time"

  "/Shopify/sarama"
  "/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, _ := ("")
  defer ()

  (ShortBytes(STREAM_MAGIC))
  (ShortBytes(STREAM_VERSION))
  ([]byte{TC_OBJECT})
  ([]byte{TC_CLASSDESC})

  className := ""
  classNameLen := len(className)

  (ShortBytes(uint16(classNameLen)))
  ([]byte(className))

  sid := -4856846361193249489

  (LongBytes(uint64(sid)))

  //flags
  ([]byte{2})

  //fields length
  (ShortBytes(2))

  //field type code
  ([]byte{'J'})

  f1 := "leastSigBits"
  f1Len := len(f1)

  (ShortBytes(uint16(f1Len)))
  ([]byte(f1))

  //filed type code
  ([]byte{'J'})

  f2 := "mostSigBits"
  f2Len := len(f2)

  (ShortBytes(uint16(f2Len)))
  ([]byte(f2))

  ([]byte{TC_ENDBLOCKDATA})
  ([]byte{TC_NULL})

  leastSigBits := -8121893460813967576

  (LongBytes(uint64(leastSigBits)))

  mostSigBits := -8810284723775779300

  (LongBytes(uint64(mostSigBits)))

}

func ShortBytes(i uint16) []byte {
  bytes := make([]byte, 2)

  .PutUint16(bytes, i)

  return bytes
}

func LongBytes(i uint64) []byte {
  bytes := make([]byte, 8)

  .PutUint64(bytes, i)

  return bytes
}

func BigEndian() { // Big endian sequence  // Binary form: 0000 0000 0000 0000 0001 0002 0003 0004  var testInt int32 = 0x01020304 // Hexadecimal representation  ("%d use big endian: \n", testInt)

  var testBytes []byte = make([]byte, 4)
  .PutUint32(testBytes, uint32(testInt)) //Big Endial Sequence Mode  ("int32 to bytes:", testBytes)

  convInt := .Uint32(testBytes) //Big Endial Sequence Mode bytes are converted to int32  ("bytes to int32: %d\n\n", convInt)
}

func LittleEndian() { // Little endian sequence  //Binary form: 0000 0000 0000 0000 0001 0002 0003 0004  var testInt int32 = 0x01020304 // Hexadecimal  ("%d use little endian: \n", testInt)

  var testBytes []byte = make([]byte, 4)
  .PutUint32(testBytes, uint32(testInt)) //Little Endial Sequence Mode  ("int32 to bytes:", testBytes)

  convInt := .Uint32(testBytes) //Byte conversion in small-endian mode  ("bytes to int32: %d\n\n", convInt)
}

func Int64ToBytes(i int64) []byte {
  var buf = make([]byte, 8)
  .PutUint64(buf, uint64(i))
  return buf
}

Java Read Test

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(""); var is = new ObjectInputStream(fis)) {
      var uuid = ();

      (uuid);

    }
  }
}

This is the article about golang's serialization method to implement java uuid. For more related content to implement java uuid serialization, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!