autoinc

package module
v2.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2025 License: MIT Imports: 3 Imported by: 0

README

autoinc Go license codecov PkgGoDev

autoinc 提供了一个简单的 ID 自增功能

ai := autoinc.New(0, 1, 1)
ctx, cancel = context.WithCancel(context.Background())
defer cancel()
go ai.Serve(ctx)

for i:=0; i<10; i++ {
    fmt.Println(ai.MustID())
}

安装

go get github.com/issue9/autoinc/v2

版权

本项目采用 MIT 开源授权许可证,完整的授权说明可在 LICENSE 文件中找到。

Documentation

Overview

Package autoinc 用于产生自增 ID

ai := autoinc.New(0, 1, 1)
ctx, cancel = context.WithCancel(context.Background())
defer cancel()

go ai.Serve(ctx)
for i:=0; i<10; i++ {
    fmt.Println(ai.ID())
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrOverflow

func ErrOverflow() error

ErrOverflow 表示自增 ID 溢出了

Types

type AutoInc

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

func New

func New(start, step int64, bufferSize int) *AutoInc

New 声明 AutoInc 实例

start:起始数值; step:步长,可以为负数,但不能为 0; bufferSize:缓存的长度。

如果 step 为 0,会直接 panic

func (*AutoInc) ID

func (ai *AutoInc) ID() (int64, bool)

ID 获取 ID 值

第二个参数若返回 false,表示当前的 ID 值已经失效。

func (*AutoInc) MustID

func (ai *AutoInc) MustID() int64

MustID 获取 ID 值

与 ID() 的不同在于,出错时会直接 panic。

func (*AutoInc) Serve

func (ai *AutoInc) Serve(ctx context.Context) error

Serve 运行该服务

这是个阻塞方法,只有此方法运行之后,AutoInc.ID 等才有返回值。

当前自增项超过最大值时,会返回 ErrOverflow 错误。

Jump to

Keyboard shortcuts

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