id

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

README

Id

id generator.

Code

generate short str by unique uint64.

Usage
go get -u github.com/snail-plus/gopkg/id
import (
	"fmt"
  "github.com/snail-plus/gopkg/id"
)

func main() {
	fmt.Println(id.NewCode(1))
	fmt.Println(id.NewCode(2))
	fmt.Println(id.NewCode(3))
	fmt.Println(id.NewCode(4))

	fmt.Println(id.NewCode(
		1,
		id.WithCodeChars([]rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}),
		id.WithCodeN1(9),
		id.WithCodeN2(3),
		id.WithCodeL(5),
		id.WithCodeSalt(99999),
	))
}
Options
  • WithCodeChars - code set, each char will generate from this set, default ['2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y']
  • WithCodeL - code length
  • WithCodeN1 - n1 & chars.length coprime
  • WithCodeN2 - n2 & code.length coprime
  • WithCodeSalt - code salt, same options and same uint64 id will generate same code, u can set different salt to generate new code

Snowflake Id

generate snowflake id based on sonyflake.

Usage
import (
	"context"
	"fmt"

  "github.com/snail-plus/gopkg/id"
)

func main() {
	sf := id.NewSonyflake(
		id.WithSonyflakeMachineId(1),
	)
	if sf.Error != nil {
		fmt.Println(sf.Error)
		return
	}
	fmt.Println(sf.Id(context.Background()))
}
Options
  • WithSonyflakeMachineId - machine id
  • WithSonyflakeStartTime - start time, do not modify after setting once, otherwise, u may get duplicate ids

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewCode

func NewCode(id uint64, options ...func(*CodeOptions)) string

NewCode can get a unique code by id(You need to ensure that id is unique).

func WithCodeChars

func WithCodeChars(arr []rune) func(*CodeOptions)

func WithCodeL

func WithCodeL(l int) func(*CodeOptions)

func WithCodeN1

func WithCodeN1(n int) func(*CodeOptions)

func WithCodeN2

func WithCodeN2(n int) func(*CodeOptions)

func WithCodeSalt

func WithCodeSalt(salt uint64) func(*CodeOptions)

func WithSonyflakeMachineId

func WithSonyflakeMachineId(id uint16) func(*SonyflakeOptions)

func WithSonyflakeStartTime

func WithSonyflakeStartTime(startTime time.Time) func(*SonyflakeOptions)

Types

type CodeOptions

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

type Sonyflake

type Sonyflake struct {
	Error error
	// contains filtered or unexported fields
}

func NewSonyflake

func NewSonyflake(options ...func(*SonyflakeOptions)) *Sonyflake

NewSonyflake can get a unique code by id(You need to ensure that id is unique).

func (*Sonyflake) Id

func (s *Sonyflake) Id(ctx context.Context) (id uint64)

type SonyflakeOptions

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

Jump to

Keyboard shortcuts

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