indigo

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2025 License: MIT Imports: 4 Imported by: 6 modules, 6 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Indigo

CI codecov Go Report Card codebeat badge Maintainability GoDoc GitHub license

About

  • A distributed unique ID generator of using Sonyflake and encoded by Base58.
  • ID max length is 11 characters by unsigned int64 max value.
  • An Encoder can change your original encoder ;)

Install

$ go get github.com/osamingo/indigo@latest

Usage

package main

import (
	"log"
	"sync"
	"time"

	"github.com/osamingo/indigo"
)

var g *indigo.Generator

func init() {
	t := time.Unix(1257894000, 0) // 2009-11-10 23:00:00 UTC
	g = indigo.New(nil, indigo.StartTime(t))
	_, err := g.NextID()
	if err != nil {
		log.Fatalln(err)
	}
}

func main() {

	wg := sync.WaitGroup{}
	wg.Add(100)
	for i := 0; i < 100; i++ {
		go func() {
			defer wg.Done()
			id, err := g.NextID()
			if err != nil {
				log.Fatalln(err)
			} else {
				log.Println("ID:", id)
			}
		}()
	}

	wg.Wait()
}

Benchmark

# Machine: MacBook Pro (14-inch, 2021)
# CPU    : Apple M1 Pro
# Memory : 32 GB

goos: darwin
goarch: arm64
pkg: github.com/osamingo/indigo
BenchmarkGenerator_NextID-10        30079       39191 ns/op        7 B/op     1 allocs/op
PASS

Bibliography

  • Sonyflake - A distributed unique ID generator inspired by Twitter's Snowflake.
  • Base58 - Base58 is a group of binary-to-text encoding schemes used to represent large integers as alphanumeric text.

License

Released under the MIT License.

Documentation

Overview

Package indigo generates a distributed unique ID generator of using Sonyflake and encoded by Base58.

More information: https://github.com/osamingo/indigo/blob/master/README.md

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckMachineID added in v1.0.1

func CheckMachineID(f func(uint16) bool) func(*sonyflake.Settings)

CheckMachineID is optional function for indigo.Generator.

func MachineID added in v1.0.1

func MachineID(f func() (uint16, error)) func(*sonyflake.Settings)

MachineID is optional function for indigo.Generator.

func StartTime added in v1.0.1

func StartTime(t time.Time) func(*sonyflake.Settings)

StartTime is optional function for indigo.Generator.

Types

type Encoder

type Encoder interface {
	Encode(i uint64) string
	Decode(s string) (uint64, error)
}

An Encoder has Encode and Decode methods.

type Generator

type Generator struct {
	// contains filtered or unexported fields
}

A Generator has sonyflake and encoder.

func New

func New(enc Encoder, options ...func(*sonyflake.Settings)) *Generator

New settings new a indigo.Generator.

func (*Generator) Decompose

func (g *Generator) Decompose(id string) (map[string]uint64, error)

Decompose returns a set of sonyflake ID parts.

func (*Generator) NextID

func (g *Generator) NextID() (string, error)

NextID generates a next unique ID.

Example
package main

import (
	"fmt"
	"time"

	"github.com/osamingo/indigo"
)

func main() { //nolint: nosnakecase
	const machineID = 65535

	g := indigo.New(
		nil,
		indigo.StartTime(time.Now()),
		indigo.MachineID(func() (uint16, error) { return machineID, nil }),
	)

	id, err := g.NextID()
	if err != nil {
		panic(err)
	}

	fmt.Println(id)

	m, err := g.Decompose(id)
	if err != nil {
		panic(err)
	}

	fmt.Println(m["machine-id"])
}
Output:
2VKmG
65535

Jump to

Keyboard shortcuts

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