helps

package
v1.1.9 Latest Latest
Warning

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

Go to latest
Published: May 13, 2024 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Base58 = "0123456789abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"                       // 58進制字串, 只使用0~9, a~z, A~Z 並且排除了oOlI這些容易混淆的字母
	Base80 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._~+-*=?!|@#$%^&<>" // 80進制字串
)
View Source
const (
	Success    = 0 // 成功
	ErrUnknown = 1 // 不明錯誤
	ErrUnwrap  = 2 // 取得錯誤編號失敗
)
View Source
const (
	FlagszOnRune  = '1' // 開啟旗標字符
	FlagszOffRune = '0' // 關閉旗標字符

)
View Source
const (
	PercentRatio100 = 100   // 百分比例值
	PercentRatio1K  = 1000  // 千分比例值
	PercentRatio10K = 10000 // 萬分比例值
)
View Source
const (
	StrNumber      = "0123456789"                              // 數字列表
	StrNumberAlpha = StrNumber + StrAlphaLower + StrAlphaUpper // 數字+小寫英文字母+大寫英文字母列表
	StrAlphaLower  = "abcdefghijklmnopqrstuvwxyz"              // 小寫英文字母列表
	StrAlphaUpper  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"              // 大寫英文字母列表
)
View Source
const (
	TimeMillisecond = time.Millisecond      // 1毫秒時間
	TimeSecond      = time.Second           // 1秒時間
	TimeMinute      = time.Minute           // 1分鐘時間
	TimeHour        = time.Hour             // 1小時時間
	TimeDay         = TimeHour * 24         // 1日時間
	TimeWeek        = TimeDay * 7           // 1週時間
	TimeMonth       = TimeWeek * 4          // 1月時間, 這個時間並不準確
	TimeYear        = TimeDay * 365         // 1年時間, 這個時間並不準確
	LayoutSecond    = "2006-01-02 15:04:05" // 時間字串格式, 使用年月日時分秒
	LayoutMinute    = "2006-01-02 15:04"    // 時間字串格式, 使用年月日時分
	LayoutDay       = "2006-01-02"          // 時間字串格式, 使用年月日

)

Variables

This section is empty.

Functions

func After added in v1.1.5

func After(u, t time.Time) bool

After 檢查 u 是否在 t 之後

func Afterf added in v1.1.1

func Afterf(layout, u string, t time.Time) bool

Afterf 檢查 u 是否在 t 之後, u 按照 layout 來解析

func Afterfx added in v1.1.5

func Afterfx(layout, u, t string) bool

Afterfx 檢查 u 是否在 t 之後, u 與 t 按照 layout 來解析

func Before added in v1.1.5

func Before(u, t time.Time) bool

Before 檢查 u 是否在 t 之前

func Beforef added in v1.1.1

func Beforef(layout, u string, t time.Time) bool

Beforef 檢查 u 是否在 t 之前, u 按照 layout 來解析

func Beforefx added in v1.1.5

func Beforefx(layout, u, t string) bool

Beforefx 檢查 u 是否在 t 之前, u 與 t 按照 layout 來解析

func Between

func Between(start, end, u time.Time, zero ...bool) bool

Between 檢查 u 是否在 start 與 end 之間; 當 start 與 end 為空時, 回傳 zero, zero 預設為true

func Betweenf

func Betweenf(layout, start, end string, u time.Time, zero ...bool) bool

Betweenf 檢查 u 是否在 start 與 end 之間, start 與 end 按照 layout 來解析; 當 start 與 end 為空時, 回傳 zero, zero 預設為true

func Betweenfx added in v1.1.5

func Betweenfx(layout, start, end, u string, zero ...bool) bool

Betweenfx 檢查 u 是否在 start 與 end 之間, start 與 end 與 u 按照 layout 來解析; 當 start 與 end 為空時, 回傳 zero, zero 預設為true

func Daily

func Daily(now, last time.Time, hour int) bool

Daily 檢查每日是否到期

func DailyNext

func DailyNext(now time.Time, hour int) time.Time

DailyNext 取得下次的每日時間

func DailyPrev

func DailyPrev(now time.Time, hour int) time.Time

DailyPrev 取得上次的每日時間

func Date

func Date(year int, month time.Month, day int, value ...int) time.Time

Date 取得指定時間, 會轉換為 SetTimeZone 設定的時區時間; 輸入參數依序是 年, 月, 日, 時, 分, 秒, 毫秒; 若時, 分, 秒, 毫秒未輸入則自動填0; 例如 Date(2023, 2, 15) 會得到 2023/02/15 00:00:00 的時間

func Err added in v1.1.0

func Err(a ...any) error

Err 產生錯誤, 最後會產生 Error 物件, 物件中分為字串與錯誤編號兩個部分, 產生的方式為

  • 字串部分: 由 a 列表中的項目轉為字串後組合而成
  • 錯誤編號部分: a 列表中最後一個可獲取的錯誤編號, 只有項目為錯誤編號或是 Error 才能獲取錯誤編號

使用時, 請讓參數結尾為 [ error | 字串 | Error, 錯誤編號]; 以下提供使用的範例

  • trials.Err(字串, 字串, ... , error, 錯誤編號)
  • trials.Err(字串, 字串, ... , error)
  • trials.Err(字串, 字串, ... , 錯誤編號)
  • trials.Err(字串, 錯誤編號)
  • trials.Err(error, 錯誤編號)
  • trials.Err(錯誤編號)

在制定錯誤編號時, 請加入以下三個預設的錯誤編號 Success, ErrUnknown, ErrUnwrap

func ExpvarStr

func ExpvarStr(expvarStat []ExpvarStat) string

ExpvarStr 取得度量字串

func FixedNext

func FixedNext(base, now time.Time, duration time.Duration) time.Time

FixedNext 取得下個固定間隔時間; base 若小於1970年, base 會被改為從1970年開始; now 若小於 base, now 會被改為 base; duration 不能小於1秒, 會造成計算錯誤

func FixedPrev

func FixedPrev(base, now time.Time, duration time.Duration) time.Time

FixedPrev 取得上個固定間隔時間; base 若小於1970年, base 會被改為從1970年開始; now 若小於 base, now 會被改為 base; duration 不能小於1秒, 會造成計算錯誤

func FlagszAdd

func FlagszAdd(input string, flag bool) string

FlagszAdd 新增旗標, 新的旗標位於尾端

func FlagszAll

func FlagszAll(input string) bool

FlagszAll 是否為全部旗標開啟

func FlagszAny

func FlagszAny(input string) bool

FlagszAny 是否為任一旗標開啟

func FlagszCount

func FlagszCount(input string, flag bool) int32

FlagszCount 取得旗標的出現數量

func FlagszGet

func FlagszGet(input string, index int32) bool

FlagszGet 取得旗標

func FlagszInit

func FlagszInit(size int32, flag bool) string

FlagszInit 初始化旗標字串

func FlagszNone

func FlagszNone(input string) bool

FlagszNone 是否為全部旗標關閉

func FlagszSet

func FlagszSet(input string, index int32, flag bool) string

FlagszSet 設定旗標, 若是旗標字串長度不足會自動填補

func FromBase58

func FromBase58(input string) (uint64, error)

FromBase58 將58進制字串轉為uint64

func FromBase80

func FromBase80(input string) (uint64, error)

FromBase80 將80進制字串轉為uint64

func FromBaseN

func FromBaseN(model, input string) (uint64, error)

FromBaseN 將n進制字串轉為uint64

func FromProtoAny added in v1.1.2

func FromProtoAny[T any](input *anypb.Any) (output *T, err error)

FromProtoAny 將any轉換為指定物件

func GetTimeZone

func GetTimeZone() *time.Location

GetTimeZone 取得時區資料

func JsonString added in v1.1.2

func JsonString(data any) string

JsonString 將json轉為字串

func Monthly

func Monthly(now, last time.Time, mday, hour int) bool

Monthly 檢查每月是否到期, mday是month-day

func MonthlyNext

func MonthlyNext(now time.Time, mday, hour int) time.Time

MonthlyNext 取得下次的每月時間, mday是month-day

func MonthlyPrev

func MonthlyPrev(now time.Time, mday, hour int) time.Time

MonthlyPrev 取得上次的每月時間, mday是month-day

func Overlap

func Overlap(start1, end1, start2, end2 time.Time) bool

Overlap 檢查兩個時間段是否有重疊

func Overlapf added in v1.1.5

func Overlapf(layout, start1, end1 string, start2, end2 time.Time) bool

Overlapf 檢查兩個時間段是否有重疊, start1 與 end1 按照 layout 來解析

func Overlapfx added in v1.1.5

func Overlapfx(layout, start1, end1, start2, end2 string) bool

Overlapfx 檢查兩個時間段是否有重疊, start1 與 end1 與 start2 與 end2 按照 layout 來解析

func ProtoString added in v1.1.2

func ProtoString(input proto.Message) string

ProtoString 將proto轉為字串

func RandInt

func RandInt() int

RandInt 取得偽隨機數值, 不會有負值

func RandInt32

func RandInt32() int32

RandInt32 取得偽隨機數值, 不會有負值

func RandInt32n

func RandInt32n(minimum, maximum int32) int32

RandInt32n 取得範圍內偽隨機數值

func RandInt64

func RandInt64() int64

RandInt64 取得偽隨機數值, 不會有負值

func RandInt64n

func RandInt64n(minimum, maximum int64) int64

RandInt64n 取得範圍內偽隨機數值

func RandIntn

func RandIntn(minimum, maximum int) int

RandIntn 取得範圍內偽隨機數值

func RandReal64

func RandReal64() int64

RandReal64 取得真隨機數值, 不需要事先設定隨機種子, 但是速度大約比偽隨機慢10倍

func RandReal64n

func RandReal64n(minimum, maximum int64) int64

RandReal64n 取得範圍內真隨機數值, 不需要事先設定隨機種子, 但是速度大約比偽隨機慢10倍

func RandSeed

func RandSeed(seed int64)

RandSeed 設定偽隨機種子

func RandSeedTime

func RandSeedTime()

RandSeedTime 設定偽隨機種子, 以目前的時間值來設定

func RandString

func RandString(length int, letter string) string

RandString 取得指定位數的隨機字串

func RandStringDefault

func RandStringDefault() string

RandStringDefault 取得預設配置的隨機字串

func ReflectFieldValue added in v1.1.9

func ReflectFieldValue[T any](value reflect.Value, name string) (result T, ok bool)

ReflectFieldValue 取得反射物件中指定欄位的值

func SetTimeZone

func SetTimeZone(timeZone string) error

SetTimeZone 設定時區資料

func SetTimeZoneLocal

func SetTimeZoneLocal()

SetTimeZoneLocal 設定時區資料為本地時間

func SetTimeZoneUTC

func SetTimeZoneUTC()

SetTimeZoneUTC 設定時區資料為UTC時間

func StrPercentage

func StrPercentage(count, total int) string

StrPercentage 取得百分比字串

func StringDisplayLength

func StringDisplayLength(input string) int

StringDisplayLength 計算字串的顯示長度, 計算的標準如下

  • 中日韓字: 2顯示長度
  • 英數及其他字: 1顯示長度

func StringJoin

func StringJoin(sep string, element ...string) string

StringJoin 以分隔字串來組合字串列表, 實際上就是使用了 strings.Join 只是轉換了參數順序而已

func TemplateBuild added in v1.1.8

func TemplateBuild(blueprint string, builder *strings.Builder, refer any) (err error)

TemplateBuild 輸入模板字串(blueprint)與參考物件(refer), 利用go語言的 text/template 引擎在字串建造器(builder)的尾端產生字串

func Time

func Time() time.Time

Time 取得現在時間, 會轉換為 SetTimeZone 設定的時區時間

func Timef added in v1.1.2

func Timef(layout, value string) (time.Time, error)

Timef 取得格式時間, 會轉換為 SetTimeZone 設定的時區時間, value 按照 layout 來解析

func ToBase58

func ToBase58(input uint64) string

ToBase58 將uint64轉為58進制字串

func ToBase80

func ToBase80(input uint64) string

ToBase80 將uint64轉為80進制字串

func ToBaseN

func ToBaseN(model string, input uint64) string

ToBaseN 將uint64轉為n進制字串

func ToProtoAny added in v1.1.2

func ToProtoAny(input ...proto.Message) (output []*anypb.Any, err error)

ToProtoAny 將proto轉換為any

func UnwrapErrID added in v1.1.0

func UnwrapErrID(err error) int

UnwrapErrID 從錯誤物件取得錯誤編號

func Weekly

func Weekly(now, last time.Time, wday, hour int) bool

Weekly 檢查每週是否到期, wday是 time.Weekday, 週日為0, 週一為1, 依此類推

func WeeklyNext

func WeeklyNext(now time.Time, wday, hour int) time.Time

WeeklyNext 取得下次的每週時間, wday是 time.Weekday, 週日為0, 週一為1, 依此類推

func WeeklyPrev

func WeeklyPrev(now time.Time, wday, hour int) time.Time

WeeklyPrev 取得上次的每週時間, wday是 time.Weekday, 週日為0, 週一為1, 依此類推

func WriteFile added in v1.1.8

func WriteFile(path string, data []byte) error

WriteFile 寫入檔案, 如果有需要會建立目錄

Types

type Dice

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

Dice 骰子資料

func NewDice

func NewDice() *Dice

NewDice 建立骰子資料

func (*Dice) Clear

func (this *Dice) Clear()

Clear 清除資料

func (*Dice) Complete

func (this *Dice) Complete(payload any, maximum int64) error

Complete 填入最後資料

func (*Dice) Fill

func (this *Dice) Fill(payload []any, weight []int64) error

Fill 填入多筆資料

func (*Dice) Max

func (this *Dice) Max() int64

Max 取得最大值

func (*Dice) One

func (this *Dice) One(payload any, weight int64) error

One 填入一筆資料

func (*Dice) Rand

func (this *Dice) Rand() any

Rand 擲骰, 最大值用內部設置

func (*Dice) Randn

func (this *Dice) Randn(maximum int64) any

Randn 擲骰, 最大值用外部設置

func (*Dice) Valid

func (this *Dice) Valid() bool

Valid 取得骰子是否有效, 如果骰子內沒有元素, 或是所有元素的權重都是0, 則此骰子無效

type DiceDetect

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

DiceDetect 骰子檢測資料

func NewDiceDetect

func NewDiceDetect() *DiceDetect

NewDiceDetect 建立骰子檢測資料

func (*DiceDetect) Add

func (this *DiceDetect) Add(key any, count int)

Add 增加檢測資料

func (*DiceDetect) Check

func (this *DiceDetect) Check(key any, total int, minimum, maximum float64) bool

Check 檢測比例是否正確, 比對的方式為檢測比例是否在min與max之間

func (*DiceDetect) Ratio

func (this *DiceDetect) Ratio(key any, total int) float64

Ratio 取得檢測比例

type Error added in v1.1.0

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

Error 錯誤資料

func (Error) Error added in v1.1.0

func (this Error) Error() string

Error 取得錯誤字串

type ExpvarStat

type ExpvarStat struct {
	Name string // 名稱
	Data any    // 資料
}

ExpvarStat 度量資料

type Fit

type Fit[T int | int32 | int64] struct {
	// contains filtered or unexported fields
}

Fit 數值檢測資料

func NewFit

func NewFit[T int | int32 | int64](maxCap, minCap FitCap[T]) *Fit[T]

NewFit 建立數值檢測資料

func (*Fit[T]) Check

func (this *Fit[T]) Check(source T, modify ...T) (result, remain, added T)

Check 數值檢測

  • 當輸入值在範圍時: result = 輸入值, remain = 0
  • 當輸入值大於上限時: result = 上限值, remain = 溢出值(正值)
  • 當輸入值小於下限時: result = 下限值, remain = 溢出值(負值)

type FitCap

type FitCap[T int | int32 | int64] func() T

FitCap 數值限制函式類型

type Percent

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

Percent 比例計算器

func NewP100

func NewP100() *Percent

NewP100 建立百分比計算器

func NewP10K

func NewP10K() *Percent

NewP10K 建立萬分比計算器

func NewP1K

func NewP1K() *Percent

NewP1K 建立千分比計算器

func NewPercent

func NewPercent(base int32) *Percent

NewPercent 建立比例計算器

func (*Percent) Add

func (this *Percent) Add(per int32) *Percent

Add 增加比例值

func (*Percent) Base

func (this *Percent) Base() int32

Base 取得基準值

func (*Percent) Calc

func (this *Percent) Calc(input int32, round Rounder) int

Calc 計算結果, input為輸入值, round為使用哪個函式來計算進位(math.Round / math.Ceil / math.Floor)

func (*Percent) Calc32

func (this *Percent) Calc32(input int32, round Rounder) int32

Calc32 計算結果, input為輸入值, round為使用哪個函式來計算進位(math.Round / math.Ceil / math.Floor)

func (*Percent) Calc64

func (this *Percent) Calc64(input int32, round Rounder) int64

Calc64 計算結果, input為輸入值, round為使用哪個函式來計算進位(math.Round / math.Ceil / math.Floor)

func (*Percent) Del

func (this *Percent) Del(per int32) *Percent

Del 減少比例值

func (*Percent) Div

func (this *Percent) Div(per int32) *Percent

Div 除以比例值

func (*Percent) Get

func (this *Percent) Get() int32

Get 取得比例值

func (*Percent) Mul

func (this *Percent) Mul(per int32) *Percent

Mul 乘以比例值

func (*Percent) Set

func (this *Percent) Set(per int32) *Percent

Set 設定比例值

func (*Percent) SetBase

func (this *Percent) SetBase() *Percent

SetBase 設定比例值為基準值

type Rounder

type Rounder func(value float64) float64

Rounder 進位函式類型

type StdColor

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

StdColor 色彩輸出顯示工具

func NewStdColor

func NewStdColor(stdout, stderr io.Writer) *StdColor

NewStdColor 建立色彩輸出顯示工具

func (*StdColor) Errf

func (this *StdColor) Errf(format string, a ...any) *StdColor

Errf 輸出錯誤訊息

func (*StdColor) Errln

func (this *StdColor) Errln(a ...any) *StdColor

Errln 輸出錯誤訊息

func (*StdColor) GetStderr

func (this *StdColor) GetStderr() io.Writer

GetStderr 取得錯誤輸出物件

func (*StdColor) GetStdout

func (this *StdColor) GetStdout() io.Writer

GetStdout 取得標準輸出物件

func (*StdColor) Outf

func (this *StdColor) Outf(format string, a ...any) *StdColor

Outf 輸出標準訊息

func (*StdColor) Outln

func (this *StdColor) Outln(a ...any) *StdColor

Outln 輸出標準訊息

type SyncAttr

type SyncAttr[T any] struct {
	// contains filtered or unexported fields
}

SyncAttr 同步屬性器

func (*SyncAttr[T]) Get

func (this *SyncAttr[T]) Get() T

Get 取得屬性物件

func (*SyncAttr[T]) Set

func (this *SyncAttr[T]) Set(attr T)

Set 設定屬性物件

type SyncOnce

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

SyncOnce 單次執行器

func (*SyncOnce) Do

func (this *SyncOnce) Do(f func()) (do bool)

Do 單次執行

func (*SyncOnce) Done

func (this *SyncOnce) Done() bool

Done 取得執行旗標

Jump to

Keyboard shortcuts

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