nanoid

A tiny, secure, URL-friendly, unique string ID generator for Go
git clone git://git.lair.cx/nanoid
Log | Files | Refs | README

commit 9708fec5c43d3b8a33dc0c9e28e9df9061f689fc
parent 3fbee55188451fb764e60ebcc7c6cab7e437fd7e
Author: Yongbin Kim <iam@yongbin.kim>
Date:   Tue, 10 Jan 2023 05:27:53 +0000

feat: Add Nil, (NanoID).Bytes().

Diffstat:
Mnanoid.go | 13+++++++++++++
1 file changed, 13 insertions(+), 0 deletions(-)

diff --git a/nanoid.go b/nanoid.go @@ -15,6 +15,10 @@ const ( type NanoID [idSize]byte +// Nil is empty id. +var Nil = NanoID{} + +// Generator generates nanoid. type Generator interface { Generate() NanoID } @@ -27,10 +31,12 @@ type generator struct { offset int } +// New returns a new generator. func New(n int) Generator { return newWithReader(cryptoRand.Reader, n) } +// NewWithReader returns a new generator with custom source. func NewWithReader(r io.Reader, n int) Generator { return newWithReader(r, n) } @@ -64,10 +70,12 @@ func (g *generator) Generate() NanoID { return *id } +// Generate generates a new id. func Generate() NanoID { return New(1).Generate() } +// GenerateN generates n new ids. func GenerateN(n int) []NanoID { gen := New(n) ids := make([]NanoID, n) @@ -83,3 +91,8 @@ func GenerateN(n int) []NanoID { func (id NanoID) String() string { return string(id[:]) } + +// Bytes returns the byte slice representation of the id. +func (id NanoID) Bytes() []byte { + return id[:] +}