diff options
author | Saumit Dinesan <justsaumit@protonmail.com> | 2023-09-04 16:19:32 +0530 |
---|---|---|
committer | Saumit Dinesan <justsaumit@protonmail.com> | 2023-09-04 16:19:32 +0530 |
commit | 117d461c4944f189576f0c4967851de458ba6f32 (patch) | |
tree | 7248b4bde0cf03e06920bf3622228652cc71b5ba /idgen/id_generator.go | |
parent | 1f61825e4b0b46d65b0a379c6b87e50d1467cfdc (diff) |
Updating README + Creating idgen package and importing it
Diffstat (limited to 'idgen/id_generator.go')
-rw-r--r-- | idgen/id_generator.go | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/idgen/id_generator.go b/idgen/id_generator.go new file mode 100644 index 0000000..8c34f41 --- /dev/null +++ b/idgen/id_generator.go @@ -0,0 +1,29 @@ +// id_generator.go +package idgen + +import ( + "math/rand" + "time" +) + +const ( + // Define the character set for the generated IDs. + chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" + + // Define the desired length of the generated IDs. + idLength = 8 +) + +// Initialize the random number generator. +func init() { + rand.Seed(time.Now().UnixNano()) +} + +// GenerateID generates a short and readable ID. +func GenerateID() string { + id := make([]byte, idLength) + for i := 0; i < idLength; i++ { + id[i] = chars[rand.Intn(len(chars))] + } + return string(id) +} |