uuid

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 22, 2023 License: MIT Imports: 6 Imported by: 0

README

le5le-com/uuid

Go Reference

A UUID package for Go, support for converting mongodb objectID to uuid.

It currently only supports UUID v7.

Installation

go get github.com/le5le-com/uuid

Usage

package main

import (
	"fmt"

	"github.com/le5le-com/uuid"
)

func main() {
	uuidv7, err := uuid.V7()
	if err != nil {
		panic(err)
	}
	fmt.Printf("UUIDv7: %s, time=%v\n", uuidv7, uuidv7.TimeFromV7())
	// UUIDv7: 0189dd43-c284-7f4f-806e-e7d238e9babb,time=2023-08-10 10:25:52.772 +0800 CST

	s := "0189dd43-c284-7f4f-806e-e7d238e9babb"
	u, err := uuid.Parse(s)
	if err != nil {
		panic(err)
	}
	fmt.Printf("uuid.Parse: %s , %s\n", s, u)

	objectId := "63ede45a8d0137fc1b631091"
	uuidv7, err = uuid.UUIDV7FromObjectID(objectId)
	if err != nil {
		panic(err)
	}
	fmt.Printf("uuid.UUIDV7FromObjectID: %s , %s\n", objectId, uuidv7)

	if uuidv7.ObjectIDHex() != objectId {
		fmt.Printf("Convert uuidv7 to objectId error: uuid.ObjectIDHex=%s, objectId=%s", uuidv7.ObjectIDHex(), objectId)
	}
}

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var InvalidFormat = "Invalid uuid format"

Functions

func V7String

func V7String() string

Types

type UUID

type UUID [16]byte

func Parse

func Parse(s string) (UUID, error)

Parse a UUID string and returns the UUID

func UUIDV7FromObjectID

func UUIDV7FromObjectID(s string) (UUID, error)

UUIDV7FromObjectID returns a UUIDv7 from mongodb objectId string

func V7

func V7() (UUID, error)

NewV7 returns a UUID Version 7 生成一个uuidv7

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                           unix_ts_ms                          |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|          unix_ts_ms           |  ver  |       rand_a          |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|var|                        rand_b                             |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                            rand_b                             |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

func (UUID) ObjectID

func (uuid UUID) ObjectID() [12]byte

ObjectID returns a mongodb objectId

func (UUID) ObjectIDHex

func (uuid UUID) ObjectIDHex() string

ObjectIDHex returns a mongodb objectId string

func (UUID) ShortString

func (uuid UUID) ShortString() string

ShortString returns a no dash hexadecimal string of a UUIDv7.

func (UUID) String

func (uuid UUID) String() string

String returns a hexadecimal string of a UUIDv7.

func (UUID) TimeFromV7

func (uuid UUID) TimeFromV7() time.Time

TimeFromV7 returns a time using time.Now hight bytes (now is 0 0).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL