You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ntool/nrandom/uuid.go

74 lines
1.4 KiB
Go

1 year ago
package nrandom
import (
"git.noahlan.cn/noahlan/ntool/nrandom/snowflake"
"github.com/gofrs/uuid/v5"
"strconv"
)
// SnowflakeId returns a snowflake uuid by default instance.
func SnowflakeId() int64 {
return snowflake.GetDefaultInstance().NextID()
}
// SnowflakeIdStr returns a snowflake uuid by default instance.
// convert to string
func SnowflakeIdStr() string {
return strconv.FormatInt(SnowflakeId(), 10)
}
// UUIDV4 returns a canonical RFC-4122 string representation of the UUID:
// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
func UUIDV4() string {
return NewUUIDV4().String()
}
// NewUUIDV7 returns a new UUID
func NewUUIDV7() uuid.UUID {
id, err := uuid.NewV7()
if err != nil {
return uuid.UUID{}
}
return id
}
// NewUUIDV6 returns a new UUID
func NewUUIDV6() uuid.UUID {
id, err := uuid.NewV6()
if err != nil {
return uuid.UUID{}
}
return id
}
// NewUUIDV4 returns a new UUID
func NewUUIDV4() uuid.UUID {
id, err := uuid.NewV4()
if err != nil {
return uuid.UUID{}
}
return id
}
// ParseUUIDSlice parses the UUID string slice to UUID slice
func ParseUUIDSlice(ids []string) []uuid.UUID {
var result []uuid.UUID
for _, v := range ids {
p, err := uuid.FromString(v)
if err != nil {
return nil
}
result = append(result, p)
}
return result
}
// ParseUUIDString parses UUID string to UUID type
func ParseUUIDString(id string) uuid.UUID {
result, err := uuid.FromString(id)
if err != nil {
return uuid.UUID{}
}
return result
}