gotool

package module
v0.0.0-...-c5de7b1 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2024 License: MIT Imports: 15 Imported by: 0

README

gotool Package

Gotool is a Go package that provides useful utility functions for common tasks such as generating random values, working with IP addresses and manipulating dates.

Installation

To use Gotool, you can simply import it in your Go project:

import "github.com/supernarsi/gotool"

Then, install the package by running:

go get github.com/supernarsi/gotool

Usage

Random

Gotool provides functions for generating random integers, booleans, and strings.

License

Gotool is released under the MIT License. See LICENSE file for details.

Documentation

Index

Constants

View Source
const (
	PRIME1 = 3          // 与字符集长度 62 互质
	PRIME2 = 5          // 与邀请码长度 6 互质
	SALT   = 4213098675 // 随意取一个数值
)

Variables

This section is empty.

Functions

func AssignGroup

func AssignGroup(id uint32, seed uint32) uint32

func CheckPasswordHash

func CheckPasswordHash(password, hash string) bool

CheckPasswordHash 校验密码与 hash

func Compare

func Compare(v1, v2 string) int

Compare 比较格式为 X.Y.Z 的版本号的大小关系(X、Y、Z 为纯数字) 返回值:0 表示v1与v2相等;1 表示v1大于v2;2 表示v1小于v2;-1 表示版本号格式错误

func Difference

func Difference[T ElementType](slice1, slice2 []T) []T

func FloatRationToInt

func FloatRationToInt(input []float32) []int

func GetUTCOffset

func GetUTCOffset(timezone string) (int, error)

func HashPassword

func HashPassword(password string) (string, error)

HashPassword 生成密码 hash

func InArray

func InArray[T ElementType](target T, arr []T) bool

InArray 判断元素是否在数组切片中

func IsSameMonthDay

func IsSameMonthDay(dateStr, timezone string, datetime time.Time, excludeSameYear bool) bool

func ListToMap

func ListToMap[T any](list []T, key string) map[int]T

func LocTimestamp

func LocTimestamp(date time.Time, timezone string) (sTimestamp, eTimestamp int64)

func LocalTime

func LocalTime(date time.Time, timezone string) (*time.Location, time.Time)

func Lottery

func Lottery(probabilities []int) int

Lottery 抽奖算法 probabilities 概率数组 return 返回概率数组中奖下标

func MergeMaps

func MergeMaps(maps ...map[string]interface{}) map[string]interface{}

MergeMaps 合并 map

func MergeMapsAny

func MergeMapsAny[K comparable, V any](maps ...map[K]V) map[K]V

MergeMapsAny 合并多个 map,适用于所有类型的 key 和 value

func RandInt

func RandInt(nums []int, dayN uint, needNum int) []int

func RandomIdx

func RandomIdx(num int) int

func RandomReadableUniCode6Len

func RandomReadableUniCode6Len() string

RandomReadableUniCode6Len 返回可读性高的 6 位唯一码

func RandomReadableUniCode8Len

func RandomReadableUniCode8Len() string

RandomReadableUniCode8Len 返回可读性高的 8 位唯一码

func RandomStrictUniCode8Len

func RandomStrictUniCode8Len() string

RandomStrictUniCode8Len 返回严格不重复的 8 位唯一码

func RandomString

func RandomString(n int) string

RandomString 随机生成 n 位字符串 重复概率较高,参考数据: 每连续生成 10 万次 6 位字符串,约产生 300 次重复 每连续生成 10 万次 8 位字符串,约产生 3 次重复

func RandomUniCode

func RandomUniCode(longCode bool, readability bool) string

RandomUniCode 随机生成 6 或 8 位不重复(非严格意义)字符串 longCode 为 true 时返回 8 位长度;false 时返回 6 位 readability 为 true 时返回只包含数字和大写字母(排除 0、O、1、I);为 false 时返回可能包含所有数字和大小写字母

重复概率参考数据: [longCode == false && readability == true]

  • 连续生成 10 万次,约 5 次重复;
  • 连续生成 100 万次,约 500 次重复;
  • 连续生成 1000 万次,约 50000 次重复

[longCode == false && readability == false]

  • 连续生成 10 万次,约 <1 次重复;
  • 连续生成 100 万次,约 10 次重复;
  • 连续生成 1000 万次,约 1000 次重复

[longCode == true && readability == true]

  • 连续生成 10 万次,约 <1 次重复;
  • 连续生成 100 万次,约 <1 次重复;
  • 连续生成 1000 万次,约 <50 次重复

[longCode == true && readability == false]

  • 理论上不重复(与 uuid 一一对应)

func SampleGenerateCode

func SampleGenerateCode(codeLength int) string

func TimeIsAfterDateEnd

func TimeIsAfterDateEnd(dateStr, timezone string, datetime time.Time) bool

func TimeIsBeforeDateBegin

func TimeIsBeforeDateBegin(dateStr, timezone string, datetime time.Time) bool

func TimeToStamp

func TimeToStamp(dateTime *time.Time, timezone string) (int64, error)

func TimeToYmdInt

func TimeToYmdInt(localTime time.Time) int

func UniInvCodeLen6ByUID

func UniInvCodeLen6ByUID(uid uint64, baseChars []byte) string

UniInvCodeLen6ByUID 根据 uid 生成 6 位的唯一码(可逆)

func UniqueElements

func UniqueElements[T ElementType](elements []T) []T

UniqueElements 过滤掉切片中个重复元素

func VerCompare

func VerCompare(v1, v2, operator string) bool

VerCompare 比较 v1、v2 的版本号大小

Types

type ElementType

type ElementType interface{ eleNum | string }

type IpDetail

type IpDetail struct {
	Ip           string
	CountryShort string
	CountryLong  string
	Province     string
	City         string
	Isp          string
	Latitude     float32
	Longitude    float32
	Zipcode      string
	Timezone     string
}

type SyncMap

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

func NewSyncMap

func NewSyncMap() *SyncMap

func (*SyncMap) IsExist

func (s *SyncMap) IsExist(key any) bool

func (*SyncMap) Lock

func (s *SyncMap) Lock(key any)

func (*SyncMap) Unlock

func (s *SyncMap) Unlock(key any)

Directories

Path Synopsis
geo

Jump to

Keyboard shortcuts

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