Skip to content

Instantly share code, notes, and snippets.

@lkebin
Created May 22, 2021 03:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lkebin/e9d174a34ecfe52bfaadadd5e2389b0b to your computer and use it in GitHub Desktop.
Save lkebin/e9d174a34ecfe52bfaadadd5e2389b0b to your computer and use it in GitHub Desktop.
MySQL UUID_TO_BIN and BIN_TO_UUID implementation in Go
package main
import (
"fmt"
"github.com/google/uuid"
)
func main() {
var id = "299feb0f-ba8d-11eb-9453-0242ac130003"
u, err := uuid.Parse(id)
if err != nil {
panic(err)
}
fmt.Printf(" original: %v\n", [16]byte(u))
bin := uuidToBin(u)
fmt.Printf("uuid_to_bin: %v\n", bin)
fmt.Printf("bin_to_uuid: %v\n", binToUuid(bin))
}
func uuidToBin(u uuid.UUID) []byte {
var b = make([]byte, 16)
copy(b[0:], u[6:8])
copy(b[2:], u[4:6])
copy(b[4:], u[:4])
copy(b[8:], u[8:])
return b
}
func binToUuid(b []byte) uuid.UUID {
var u uuid.UUID
copy(u[0:], b[4:8])
copy(u[4:], b[2:4])
copy(u[6:], b[:2])
copy(u[8:], b[8:])
return u
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment