oklog/ulid — dự án mã nguồn mở — đang có 5.1k sao GitHub. Universally Unique Lexicographically Sortable Identifier (ULID) in Go
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
A Go port of ulid/javascript with binary format implemented.
Background
A GUID/UUID can be suboptimal for many use-cases because:
It isn't the most character efficient way of encoding 128 bits
UUID v1/v2 is impractical in many environments, as it requires access to a unique, stable MAC address
UUID v3/v5 requires a unique seed and produces randomly distributed IDs, which can cause fragmentation in many data structures
UUID v4 provides no other information than randomness which can cause fragmentation in many data structures
A ULID however:
Is compatible with UUID/GUID's
1.21e+24 unique ULIDs per millisecond (1,208,925,819,614,629,174,706,176 to be exact)
Lexicographically sortable
Canonically encoded as a 26 character string, as opposed to the 36 character UUID
Uses Crockford's base32 for better efficiency and readability (5 bits per character)
Case insensitive
No special characters (URL safe)
Monotonic sort order (correctly detects and handles the same millisecond)
Install
This package requires Go modules.
go get github.com/oklog/ulid/v2
Usage
ULIDs are constructed from two things: a timestamp with millisecond precision,
and some random data.
Timestamps are modeled as uint64 values representing a Unix time in milliseconds.
They can be produced by passing a time.Time to
ulid.Timestamp,
or by calling time.Time.UnixMilli
and converting the returned value to uint64.
Random data is taken from a provided io.Reader.
This design allows for greater flexibility when choosing trade-offs, but can be
a bit confusing to newcomers.
If you just want to generate a ULID and don't (yet) care about details like
performance, cryptographic security, etc., use the
ulid.Make helper function.
This function calls time.Now to get a timestamp,
and uses a source of entropy which is process-global,
pseudo-random, and
monotonic.
entropy := rand.New(rand.NewSource(time.Now().UnixNano()))
ms := ulid.Timestamp(time.Now())
fmt.Println(ulid.New(ms, entropy))
// 01G65Z755AFWAKHE12NY0CQ9FH
Care should be taken when providing a source of entropy.
The above example utilizes math/rand.Rand,
which is not safe for concurrent use by multiple goroutines. Consider
alternatives such as
x/exp/rand.
Security-sensitive use cases should always use cryptographically secure entropy
provided by crypto/rand.
Performance-sensitive use cases should avoid synchronization when generating
IDs. One option is to use a unique source of entropy for each concurrent
goroutine, which results in no lock contention, but cannot provide strong
guarantees about the random data, and does not provide monotonicity within a
given millisecond. One common performance optimization is to pool sources of
entropy using a sync.Pool.
Monotonicity is a property that says each ULID is "bigger than" the previous
one. ULIDs are automatically monotonic, but only to millisecond precision. ULIDs
generated within the same millisecond are ordered by their random component,
which means they are by default un-ordered. You can use
ulid.MonotonicEntropy or
ulid.LockedMonotonicEntropy
to create ULIDs that are monotonic within a given millisecond, with caveats. See
the documentation for details.
If you don't care about time-based ordering of generated IDs, then there's no
reason to use ULIDs! There are many other kinds of IDs that are easier, faster,
smaller, etc. Consider UUIDs.
Commandline tool
This repo also provides a tool to generate and parse ULIDs at the command line.
go install github.com/oklog/ulid/v2/cmd/ulid@latest
Usage:
Usage: ulid [-hlqz] [-f <format>] [parameters ...]
-f, --format=<format> when parsing, show times in this format: default, rfc3339, unix, ms
-h, --help print this help text
-l, --local when parsing, show local time instead of UTC
-q, --quick when generating, use non-crypto-grade entropy
-z, --zero when generating, fix entropy to all-zeroes
Examples:
$ ulid
01D78XYFJ1PRM1WPBCBT3VHMNV
$ ulid -z
01D78XZ44G0000000000000000
$ ulid 01D78XZ44G0000000000000000
Sun Mar 31 03:51:23.536 UTC 2019
$ ulid --format=rfc3339 --local 01D78XZ44G0000000000000000
2019-03-31T05:51:23.536+02:00
Specification
Below is the current specification of ULID as implemented in this repository.
Components
Timestamp
48 bits
UNIX-time in milliseconds
Won't run out of space till the year 10889 AD
Entropy
80 bits
User defined entropy source.
Monotonicity within the same millisecond with ulid.Monotonic
Encoding
Crockford's Base32 is used as shown.
This alphabet excludes the letters I, L, O, and U to avoid confusion and abuse.
0123456789ABCDEFGHJKMNPQRSTVWXYZ
Binary Layout and Byte Order
The components are encoded as 16 octets. Each component is encoded with the Most Significant Byte first (network byte order).
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/oklog/ulid là nguồn chính thức.
oklog/ulid có bao nhiêu sao?
oklog/ulid có 5.1k sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/oklog/ulid. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
oklog/ulid có phải mã nguồn mở không?
Có — oklog/ulid phát hành theo license Apache-2.0, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/oklog/ulid.
oklog/ulid còn đang phát triển không?
Commit gần nhất trên oklog/ulid là 18 ngày trước (theo timestamp GitHub). Repo có 195 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
oklog/ulid dùng license gì?
oklog/ulid phát hành theo license Apache-2.0. Nên mở file LICENSE trên GitHub để xác nhận — license metadata đôi khi lệch với thực tế dự án.
oklog/ulid là gì?
oklog/ulid (oklog/ulid) là dự án Go trên GitHub. Theo mô tả gốc: Universally Unique Lexicographically Sortable Identifier (ULID) in Go
oklog/ulid viết bằng ngôn ngữ gì?
oklog/ulid chủ yếu viết bằng Go. Trường "language" của GitHub dựa trên phần lớn byte ở nhánh mặc định.
Đọc đầy đủ README ở tab phía trên.
Vẫn đang phân vân về ulid?
Một cú bấm sẽ gửi câu hỏi kèm trang này cho AI — xem AI nói gì về ulid.