Documentation ¶
Overview ¶
Package shortid enables the generation of short, unique, non-sequential and by default URL friendly Ids. The package is heavily inspired by the node.js https://github.com/dylang/shortid library.
Id Length ¶
The standard Id length is 9 symbols when generated at a rate of 1 Id per millisecond, occasionally it reaches 11 (at the rate of a few thousand Ids per millisecond) and very-very rarely it can go beyond that during continuous generation at full throttle on high-performant hardware. A test generating 500k Ids at full throttle on conventional hardware generated the following Ids at the head and the tail (length > 9 is expected for this test):
-NDveu-9Q iNove6iQ9J NVDve6-9Q VVDvc6i99J NVovc6-QQy VVoveui9QC ... tFmGc6iQQs KpTvcui99k KFTGcuiQ9p KFmGeu-Q9O tFTvcu-QQt tpTveu-99u
Life span ¶
The package guarantees the generation of unique Ids with zero collisions for 34 years (1/1/2016-1/1/2050) using the same worker Id within a single (although concurrent) application if application restarts take longer than 1 millisecond. The package supports up to 32 works, all providing unique sequences.
Implementation details ¶
Although heavily inspired by the node.js shortid library this is not a simple Go port. In addition it
- is safe to concurrency;
- does not require any yearly version/epoch resets;
- provides stable Id size over a long period at the rate of 1ms;
- guarantees no collisions (due to guaranteed fixed size of Ids between milliseconds and because multiple requests within the same ms lead to longer Ids with the prefix unique to the ms);
- supports 32 over 16 workers.
The algorithm uses less randomness than the original node.js implementation, which permits to extend the life span as well as reduce and guarantee the length. In general terms, each Id has the following 3 pieces of information encoded: the millisecond (first 8 symbols), the worker Id (9th symbol), running concurrent counter within the same millisecond, only if required, over all remaining symbols. The element of randomness per symbol is 1/2 for the worker and the millisecond and 0 for the counter. Here 0 means no randomness, i.e. every value is encoded using a 64-base alphabet; 1/2 means one of two matching symbols of the supplied alphabet, 1/4 one of four matching symbols. The original algorithm of the node.js module uses 1/4 throughout.
All methods accepting the parameters that govern the randomness are exported and can be used to directly implement an algorithm with e.g. more randomness, but with longer Ids and shorter life spans.
Index ¶
- Constants
- Variables
- func AddDate(t time.Time, years, months, days int) time.Time
- func AddDateP(t *time.Time, years, months, days int)
- func All[T any](set []T) (subsets [][]T)
- func Combinations[T any](set []T, n int) (subsets [][]T)
- func ConvertBigIntFromString(v0, v1 string) (n0 *big.Int, n1 *big.Int, err error)
- func ConvertBigIntTokenAmountToFloat64(b *big.Int, tokenDecimal int32) float64
- func ConvertFloat64ToTokenAmount(amount float64, decimals int) *big.Int
- func ConvertHexToDecimalInStringFormat(hexString string) string
- func ConvertTokenAmountToFloat64(amt string, tokenDecimal int32) float64
- func CreateTransactionOpts(client *ethclient.Client, key *ecdsa.PrivateKey, chainId uint64, ...) (opts *bind.TransactOpts, err error)
- func Date(year int, month time.Month, day int, loc ...*time.Location) time.Time
- func DecodeInputData(abiStr string, inputData string) map[string]interface{}
- func EncodeAddress(addr common.Address) []byte
- func EncodeBigInt(b *big.Int) []byte
- func EncodeBytesString(v string) []byte
- func EncodePacked(input ...[]byte) []byte
- func EncodeUint256(v string) []byte
- func EncodeUint256Array(arr []string) []byte
- func GenerateIncreaseID() (int64, error)
- func GenerateShortId() (string, error)
- func GetBigIntFromString(v0 string) (n0 *big.Int, err error)
- func GetEthCallPostData(to string, data string) string
- func GetHTTPClient() *http.Client
- func GetRandomNumber(min, max int) (int, error)
- func GetTokenPriceUSDT(okxurl string, base, quote string) (float64, error)
- func HHMM(t time.Time, seperator string) string
- func HHMMSS(t time.Time, seperator string, decimal ...int) string
- func HTTPReq(method string, url string, httpClient *http.Client, content []byte, ...) (body []byte, err error)
- func InTransaction(db *xorm.Engine, callback dbTransactionFunc) error
- func InitLog(logPath, logLever string)
- func IsAnAddress(address string) bool
- func IsValidTxHash(txHash string) bool
- func Logger() *zap.Logger
- func MustGenerateShortId() string
- func Pad(s string, n int, r rune) (string, error)
- func PadChar(s string, n int, r rune) (string, error)
- func PadLeft(str, pad string, length int) string
- func PrivateToAddress(key string) (string, error)
- func ProcUpDuration() time.Duration
- func RemoveIndex[T any](s []T, index int) []T
- func SetDefault(sid *Shortid)
- func TxWaitToSync(ctx context.Context, client *ethclient.Client, tx *types.Transaction) (*types.Receipt, bool, error)
- func UnixMicro(t time.Time) int64
- func UnixMilli(t time.Time) int64
- func YYMMDD(t time.Time, seperator string) string
- func YYYYMMDD(t time.Time, seperator string) string
- type Abc
- type CycledStringArray
- type EthCallResult
- type Shortid
- func (sid *Shortid) Abc() Abc
- func (sid *Shortid) Epoch() time.Time
- func (sid *Shortid) Generate() (string, error)
- func (sid *Shortid) GenerateInternal(tm *time.Time, epoch time.Time) (string, error)
- func (sid *Shortid) MustGenerate() string
- func (sid *Shortid) String() string
- func (sid *Shortid) Worker() uint
- type Ticker
- type TickerData
Examples ¶
Constants ¶
const DefaultABC = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-"
DefaultABC is the default URL-friendly alphabet.
const Version = 1.1
Version defined the library version.
Variables ¶
var IsAlphanumeric = regexp.MustCompile(`^[0-9a-zA-Z]+$`).MatchString
Functions ¶
func AddDate ¶ added in v1.0.54
AddDate returns the time corresponding to adding the given number of years, months, and days to t. For example, AddDate(-1, 2, 3) applied to January 1, 2011 returns March 4, 2010.
However, this version of AddDate will examine the date of month. For example, AddDate(0, 1, 0) applied to January 31, 2011 returns Feburary 28, 2011 instead of March 3, 2011.
func All ¶
func All[T any](set []T) (subsets [][]T)
All returns all combinations for a given string array. This is essentially a powerset of the given set except that the empty set is disregarded.
Example ¶
combinations := All([]string{"A", "B", "C"}) fmt.Println(combinations)
Output: [[A] [B] [A B] [C] [A C] [B C] [A B C]]
func Combinations ¶
Combinations returns combinations of n elements for a given string array. For n < 1, it equals to All and returns all combinations.
func ConvertBigIntFromString ¶
func ConvertBigIntTokenAmountToFloat64 ¶ added in v1.0.63
func CreateTransactionOpts ¶
func Date ¶ added in v1.0.54
Date returns a time.Time value with year, month, day and location only. If not indecating loc, UTC will be used.
func DecodeInputData ¶
func EncodeAddress ¶
func EncodeBigInt ¶
func EncodeBytesString ¶
func EncodePacked ¶
func EncodeUint256 ¶
func EncodeUint256Array ¶
func GenerateIncreaseID ¶
func GenerateShortId ¶
Generate generates an Id using the default generator.
func GetBigIntFromString ¶ added in v1.0.34
func GetEthCallPostData ¶
func GetHTTPClient ¶
func GetTokenPriceUSDT ¶ added in v1.0.48
func HHMMSS ¶ added in v1.0.54
HHMMSS is equivalent to t.Format(fmt.Sprintf("03%s04%s05", seperator, seperator)). With decimal, additional decimal digits of seconds will be added after a dot.
func HTTPReq ¶
func HTTPReq(method string, url string, httpClient *http.Client, content []byte, headers map[string]string) (body []byte, err error)
HTTPReq ..
func InTransaction ¶
func IsAnAddress ¶
func IsValidTxHash ¶
func MustGenerateShortId ¶
func MustGenerateShortId() string
MustGenerate acts just like Generate, but panics instead of returning errors.
func PadChar ¶
PadChar left-pads s with the rune r, to length n. If n is smaller than s, PadChar is a no-op.
func PrivateToAddress ¶
func ProcUpDuration ¶ added in v1.0.54
ProcUpDuration returns a rough duration indecating how long current process has run.
func RemoveIndex ¶
func TxWaitToSync ¶
Types ¶
type Abc ¶
type Abc struct {
// contains filtered or unexported fields
}
Abc represents a shuffled alphabet used to generate the Ids and provides methods to encode data.
func MustNewAbc ¶
MustNewAbc acts just like NewAbc, but panics instead of returning errors.
func NewAbc ¶
NewAbc constructs a new instance of shuffled alphabet to be used for Id representation.
func (*Abc) Encode ¶
Encode encodes a given value into a slice of runes of length nsymbols. In case nsymbols==0, the length of the result is automatically computed from data. Even if fewer symbols is required to encode the data than nsymbols, all positions are used encoding 0 where required to guarantee uniqueness in case further data is added to the sequence. The value of digits [4,6] represents represents n in 2^n, which defines how much randomness flows into the algorithm: 4 -- every value can be represented by 4 symbols in the alphabet (permitting at most 16 values), 5 -- every value can be represented by 2 symbols in the alphabet (permitting at most 32 values), 6 -- every value is represented by exactly 1 symbol with no randomness (permitting 64 values).
func (*Abc) MustEncode ¶
MustEncode acts just like Encode, but panics instead of returning errors.
type CycledStringArray ¶
func CreateCycledStringArray ¶
func CreateCycledStringArray(strs []string) *CycledStringArray
func (*CycledStringArray) GetCurrentString ¶
func (csa *CycledStringArray) GetCurrentString() string
type EthCallResult ¶
type EthCallResult struct { Jsonrpc string `json:"jsonrpc"` Result string `json:"result"` ID int `json:"id"` }
func ReadContract ¶
func ReadContract(url string, postdata string) (*EthCallResult, error)
type Shortid ¶
type Shortid struct {
// contains filtered or unexported fields
}
Shortid type represents a short Id generator working with a given alphabet.
func GetDefault ¶
func GetDefault() *Shortid
GetDefault retrieves the default short Id generator initialised with the default alphabet, worker=0 and seed=1. The default can be overwritten using SetDefault.
func New ¶
New constructs an instance of the short Id generator for the given worker number [0,31], alphabet (64 unique symbols) and seed value (to shuffle the alphabet). The worker number should be different for multiple or distributed processes generating Ids into the same data space. The seed, on contrary, should be identical.
func (*Shortid) Epoch ¶
Epoch returns the value of epoch used as the beginning of millisecond counting (normally 2016-01-01 00:00:00 local time)
func (*Shortid) GenerateInternal ¶
GenerateInternal should only be used for testing purposes.
func (*Shortid) MustGenerate ¶
MustGenerate acts just like Generate, but panics instead of returning errors.
type Ticker ¶ added in v1.0.48
type Ticker struct { Code string `json:"code"` Msg string `json:"msg"` Data []*TickerData `json:"data"` }