From 4c5524354c08db196dd930a037a003f2b8edce99 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Fri, 13 Jan 2023 14:36:33 +0800 Subject: [PATCH] doc: normalize documents --- docs/datetime.md | 470 ++++++++++++++++++++++++----------- docs/datetime_zh-CN.md | 539 ++++++++++++++++++++++++++-------------- docs/fileutil.md | 102 ++++---- docs/fileutil_zh-CN.md | 102 ++++---- docs/formatter.md | 25 +- docs/formatter_zh-CN.md | 27 +- docs/function.md | 270 ++++++++++---------- docs/function_zh-CN.md | 275 ++++++++++---------- docs/maputil.md | 303 ++++++++++++---------- docs/maputil_zh-CN.md | 305 +++++++++++++---------- docs/mathutil.md | 297 ++++++++++++++-------- docs/mathutil_zh-CN.md | 296 ++++++++++++++-------- 12 files changed, 1800 insertions(+), 1211 deletions(-) diff --git a/docs/datetime.md b/docs/datetime.md index d48ee2b..fcf45bb 100644 --- a/docs/datetime.md +++ b/docs/datetime.md @@ -1,16 +1,18 @@ # Datetime + Package datetime supports date and time format and compare.
## Source: -- [https://github.com/duke-git/lancet/blob/main/datetime/datetime.go](https://github.com/duke-git/lancet/blob/main/datetime/datetime.go) -- [https://github.com/duke-git/lancet/blob/main/datetime/conversion.go](https://github.com/duke-git/lancet/blob/main/datetime/conversion.go) +- [https://github.com/duke-git/lancet/blob/main/datetime/datetime.go](https://github.com/duke-git/lancet/blob/main/datetime/datetime.go) +- [https://github.com/duke-git/lancet/blob/main/datetime/conversion.go](https://github.com/duke-git/lancet/blob/main/datetime/conversion.go)
## Usage: + ```go import ( "github.com/duke-git/lancet/v2/datetime" @@ -20,66 +22,67 @@ import (
## Index -- [AddDay](#AddDay) -- [AddHour](#AddHour) -- [AddMinute](#AddMinute) -- [BeginOfMinute](#BeginOfMinute) -- [BeginOfHour](#BeginOfHour) -- [BeginOfDay](#BeginOfDay) -- [BeginOfWeek](#BeginOfWeek) -- [BeginOfMonth](#BeginOfMonth) -- [BeginOfYear](#BeginOfYear) -- [EndOfMinute](#EndOfMinute) -- [EndOfHour](#EndOfHour) -- [EndOfDay](#EndOfDay) -- [EndOfWeek](#EndOfWeek) -- [EndOfMonth](#EndOfMonth) -- [EndOfYear](#EndOfYear) -- [GetNowDate](#GetNowDate) -- [GetNowTime](#GetNowTime) -- [GetNowDateTime](#GetNowDateTime) -- [GetZeroHourTimestamp](#GetZeroHourTimestamp) -- [GetNightTimestamp](#GetNightTimestamp) -- [FormatTimeToStr](#FormatTimeToStr) -- [FormatStrToTime](#FormatStrToTime) -- [NewUnixNow](#NewUnixNow) -- [NewUnix](#NewUnix) -- [NewFormat](#NewFormat) -- [NewISO8601](#NewISO8601) -- [ToUnix](#ToUnix) -- [ToFormat](#ToFormat) -- [ToFormatForTpl](#ToFormatForTpl) -- [ToIso8601](#ToIso8601) - +- [AddDay](#AddDay) +- [AddHour](#AddHour) +- [AddMinute](#AddMinute) +- [BeginOfMinute](#BeginOfMinute) +- [BeginOfHour](#BeginOfHour) +- [BeginOfDay](#BeginOfDay) +- [BeginOfWeek](#BeginOfWeek) +- [BeginOfMonth](#BeginOfMonth) +- [BeginOfYear](#BeginOfYear) +- [EndOfMinute](#EndOfMinute) +- [EndOfHour](#EndOfHour) +- [EndOfDay](#EndOfDay) +- [EndOfWeek](#EndOfWeek) +- [EndOfMonth](#EndOfMonth) +- [EndOfYear](#EndOfYear) +- [GetNowDate](#GetNowDate) +- [GetNowTime](#GetNowTime) +- [GetNowDateTime](#GetNowDateTime) +- [GetZeroHourTimestamp](#GetZeroHourTimestamp) +- [GetNightTimestamp](#GetNightTimestamp) +- [FormatTimeToStr](#FormatTimeToStr) +- [FormatStrToTime](#FormatStrToTime) +- [NewUnixNow](#NewUnixNow) +- [NewUnix](#NewUnix) +- [NewFormat](#NewFormat) +- [NewISO8601](#NewISO8601) +- [ToUnix](#ToUnix) +- [ToFormat](#ToFormat) +- [ToFormatForTpl](#ToFormatForTpl) +- [ToIso8601](#ToIso8601)
## Documentation ## Note: -1. 'format' string param in func FormatTimeToStr and FormatStrToTime function should be one of flows: -- yyyy-mm-dd hh:mm:ss -- yyyy-mm-dd hh:mm -- yyyy-mm-dd hh -- yyyy-mm-dd -- yyyy-mm -- mm-dd -- dd-mm-yy hh:mm:ss -- yyyy/mm/dd hh:mm:ss -- yyyy/mm/dd hh:mm -- yyyy-mm-dd hh -- yyyy/mm/dd -- yyyy/mm -- mm/dd -- dd/mm/yy hh:mm:ss -- yyyy -- mm -- hh:mm:ss -- mm:ss +1. 'format' string param in func FormatTimeToStr and FormatStrToTime function should be one of flows: + +- yyyy-mm-dd hh:mm:ss +- yyyy-mm-dd hh:mm +- yyyy-mm-dd hh +- yyyy-mm-dd +- yyyy-mm +- mm-dd +- dd-mm-yy hh:mm:ss +- yyyy/mm/dd hh:mm:ss +- yyyy/mm/dd hh:mm +- yyyy-mm-dd hh +- yyyy/mm/dd +- yyyy/mm +- mm/dd +- dd/mm/yy hh:mm:ss +- yyyy +- mm +- hh:mm:ss +- mm:ss ### AddDay +

Add or sub days to time.

Signature: @@ -87,6 +90,7 @@ import ( ```go func AddDay(t time.Time, day int64) time.Time ``` + Example: ```go @@ -100,15 +104,24 @@ import ( func main() { now := time.Now() - after2Days := datetime.AddDay(now, 2) - before2Days := datetime.AddDay(now, -2) - fmt.Println(after2Days, before2Days) + tomorrow := datetime.AddDay(now, 1) + diff1 := tomorrow.Sub(now) + + yesterday := datetime.AddDay(now, -1) + diff2 := yesterday.Sub(now) + + fmt.Println(diff1) + fmt.Println(diff2) + + // Output: + // 24h0m0s + // -24h0m0s } ``` - ### AddHour +

Add or sub hours to time.

Signature: @@ -116,6 +129,7 @@ func main() { ```go func AddHour(t time.Time, hour int64) time.Time ``` + Example: ```go @@ -129,14 +143,24 @@ import ( func main() { now := time.Now() - after2Hours := datetime.AddHour(now, 2) - before2Hours := datetime.AddHour(now, -2) - fmt.Println(after2Hours, after2Hours) + after2Hours := datetime.AddHour(now, 2) + diff1 := after2Hours.Sub(now) + + before2Hours := datetime.AddHour(now, -2) + diff2 := before2Hours.Sub(now) + + fmt.Println(diff1) + fmt.Println(diff2) + + // Output: + // 2h0m0s + // -2h0m0s } ``` ### AddMinute +

Add or sub minutes to time.

Signature: @@ -144,6 +168,7 @@ func main() { ```go func AddMinute(t time.Time, minute int64) time.Time ``` + Example: ```go @@ -157,14 +182,24 @@ import ( func main() { now := time.Now() - after2Minute := datetime.AddMinute(now, 2) - before2Minute := datetime.AddMinute(now, -2) - fmt.Println(after2Minute, before2Minute) + after2Minutes := datetime.AddMinute(now, 2) + diff1 := after2Minutes.Sub(now) + + before2Minutes := datetime.AddMinute(now, -2) + diff2 := before2Minutes.Sub(now) + + fmt.Println(diff1) + fmt.Println(diff2) + + // Output: + // 2m0s + // -2m0s } ``` ### BeginOfMinute +

Return beginning minute time of day.

Signature: @@ -172,6 +207,7 @@ func main() { ```go func BeginOfMinute(t time.Time) time.Time ``` + Example: ```go @@ -184,13 +220,18 @@ import ( ) func main() { - td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local) - bm := datetime.BeginOfMinute(td) - fmt.Println(bm) //2022-02-15 15:48:00 +0800 CST + input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC) + result := datetime.BeginOfMinute(input) + + fmt.Println(result) + + // Output: + // 2023-01-08 18:50:00 +0000 UTC } ``` ### BeginOfHour +

Return zero time of day.

Signature: @@ -198,6 +239,7 @@ func main() { ```go func BeginOfHour(t time.Time) time.Time ``` + Example: ```go @@ -210,13 +252,18 @@ import ( ) func main() { - td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local) - bm := datetime.BeginOfHour(td) - fmt.Println(bm) //2022-02-15 15:00:00 +0800 CST + input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC) + result := datetime.BeginOfHour(input) + + fmt.Println(result) + + // Output: + // 2023-01-08 18:00:00 +0000 UTC } ``` ### BeginOfDay +

Return begin time of day.

Signature: @@ -224,6 +271,7 @@ func main() { ```go func BeginOfDay(t time.Time) time.Time ``` + Example: ```go @@ -236,15 +284,18 @@ import ( ) func main() { - td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local) - bm := datetime.BeginOfDay(td) - fmt.Println(bm) //2022-02-15 00:00:00 +0800 CST + input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC) + result := datetime.BeginOfDay(input) + + fmt.Println(result) + + // Output: + // 2023-01-08 00:00:00 +0000 UTC } ``` - - ### BeginOfWeek +

Return beginning time of week, week begin from Sunday.

Signature: @@ -252,6 +303,7 @@ func main() { ```go func BeginOfWeek(t time.Time, beginFrom ...time.Weekday) time.Time ``` + Example: ```go @@ -264,15 +316,18 @@ import ( ) func main() { - td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local) - bm := datetime.BeginOfWeek(td) - fmt.Println(bm) //2022-02-13 00:00:00 +0800 CST + input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC) + result := datetime.BeginOfWeek(input) + + fmt.Println(result) + + // Output: + // 2023-01-08 00:00:00 +0000 UTC } ``` - - ### BeginOfMonth +

Return beginning time of month

Signature: @@ -280,6 +335,7 @@ func main() { ```go func BeginOfMonth(t time.Time) time.Time ``` + Example: ```go @@ -292,14 +348,18 @@ import ( ) func main() { - td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local) - bm := datetime.BeginOfMonth(td) - fmt.Println(bm) //2022-02-01 00:00:00 +0800 CST + input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC) + result := datetime.BeginOfMonth(input) + + fmt.Println(result) + + // Output: + // 2023-01-01 00:00:00 +0000 UTC } ``` - ### BeginOfYear +

Return beginning time of year.

Signature: @@ -307,6 +367,7 @@ func main() { ```go func BeginOfYear(t time.Time) time.Time ``` + Example: ```go @@ -319,15 +380,18 @@ import ( ) func main() { - td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local) - bm := datetime.BeginOfYear(td) - fmt.Println(bm) //2022-01-01 00:00:00 +0800 CST + input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC) + result := datetime.BeginOfYear(input) + + fmt.Println(result) + + // Output: + // 2023-01-01 00:00:00 +0000 UTC } ``` - - ### EndOfMinute +

Return end time minute of day.

Signature: @@ -335,6 +399,7 @@ func main() { ```go func EndOfMinute(t time.Time) time.Time ``` + Example: ```go @@ -347,13 +412,18 @@ import ( ) func main() { - td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local) - bm := datetime.EndOfMinute(td) - fmt.Println(bm) //2022-02-15 15:48:59.999999999 +0800 CST + input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC) + result := datetime.EndOfMinute(input) + + fmt.Println(result) + + // Output: + // 2023-01-08 18:50:59.999999999 +0000 UTC } ``` ### EndOfHour +

Return end time hour of day.

Signature: @@ -361,6 +431,7 @@ func main() { ```go func EndOfHour(t time.Time) time.Time ``` + Example: ```go @@ -373,13 +444,18 @@ import ( ) func main() { - td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local) - bm := datetime.EndOfHour(td) - fmt.Println(bm) //2022-02-15 15:59:59.999999999 +0800 CST + input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC) + result := datetime.EndOfHour(input) + + fmt.Println(result) + + // Output: + // 2023-01-08 18:59:59.999999999 +0000 UTC } ``` ### EndOfDay +

Return end time hour of day.

Signature: @@ -387,6 +463,7 @@ func main() { ```go func EndOfDay(t time.Time) time.Time ``` + Example: ```go @@ -399,15 +476,18 @@ import ( ) func main() { - td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local) - bm := datetime.EndOfDay(td) - fmt.Println(bm) //2022-02-15 23:59:59.999999999 +0800 CST + input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC) + result := datetime.EndOfDay(input) + + fmt.Println(result) + + // Output: + // 2023-01-08 23:59:59.999999999 +0000 UTC } ``` - - ### EndOfWeek +

Return end time of week, week end with Saturday.

Signature: @@ -415,6 +495,7 @@ func main() { ```go func EndOfWeek(t time.Time, endWith ...time.Weekday) time.Time ``` + Example: ```go @@ -427,15 +508,18 @@ import ( ) func main() { - td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local) - bm := datetime.EndOfWeek(td) - fmt.Println(bm) //2022-02-19 23:59:59.999999999 +0800 CST + input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC) + result := datetime.EndOfWeek(input) + + fmt.Println(result) + + // Output: + // 2023-01-14 23:59:59.999999999 +0000 UTC } ``` - - ### EndOfMonth +

Return end time of month

Signature: @@ -443,6 +527,7 @@ func main() { ```go func EndOfMonth(t time.Time) time.Time ``` + Example: ```go @@ -455,14 +540,18 @@ import ( ) func main() { - td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local) - bm := datetime.EndOfMonth(td) - fmt.Println(bm) //2022-02-28 23:59:59.999999999 +0800 CST + input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC) + result := datetime.EndOfMonth(input) + + fmt.Println(result) + + // Output: + // 2023-01-31 23:59:59.999999999 +0000 UTC } ``` - ### EndOfYear +

Return beginning time of year.

Signature: @@ -470,6 +559,7 @@ func main() { ```go func EndOfYear(t time.Time) time.Time ``` + Example: ```go @@ -482,14 +572,18 @@ import ( ) func main() { - td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local) - bm := datetime.EndOfYear(td) - fmt.Println(bm) //2022-12-31 23:59:59.999999999 +0800 CST + input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC) + result := datetime.EndOfYear(input) + + fmt.Println(result) + + // Output: + // 2023-12-31 23:59:59.999999999 +0000 UTC } ``` - ### GetNowDate +

Get current date string, format is yyyy-mm-dd.

Signature: @@ -497,6 +591,7 @@ func main() { ```go func GetNowDate() string ``` + Example: ```go @@ -511,12 +606,16 @@ import ( func main() { now := time.Now() currentDate := datetime.GetNowDate() - fmt.Println(currentDate) // 2022-01-28 + + fmt.Println(currentDate) + + // Output: + // 2022-01-28 } ``` - ### GetNowTime +

Get current time string, format is hh:mm:ss.

Signature: @@ -524,6 +623,7 @@ func main() { ```go func GetNowTime() string ``` + Example: ```go @@ -538,12 +638,16 @@ import ( func main() { now := time.Now() currentTime := datetime.GetNowTime() - fmt.Println(currentDate) // 15:57:33 + + fmt.Println(currentTime) // 15:57:33 + + // Output: + // 15:57:33 } ``` - ### GetNowDateTime +

Get current date time string, format is yyyy-mm-dd hh:mm:ss.

Signature: @@ -551,6 +655,7 @@ func main() { ```go func GetNowDateTime() string ``` + Example: ```go @@ -565,12 +670,16 @@ import ( func main() { now := time.Now() current := datetime.GetNowDateTime() - fmt.Println(current) // 2022-01-28 15:59:33 + + fmt.Println(current) + + // Output: + // 2022-01-28 15:59:33 } ``` - ### GetZeroHourTimestamp +

Return timestamp of zero hour (timestamp of 00:00).

Signature: @@ -578,6 +687,7 @@ func main() { ```go func GetZeroHourTimestamp() int64 ``` + Example: ```go @@ -592,12 +702,16 @@ import ( func main() { now := time.Now() zeroTime := datetime.GetZeroHourTimestamp() - fmt.Println(zeroTime) // 1643299200 + + fmt.Println(zeroTime) + + // Output: + // 1643299200 } ``` - ### GetNightTimestamp +

Return timestamp of zero hour (timestamp of 23:59).

Signature: @@ -605,6 +719,7 @@ func main() { ```go func GetNightTimestamp() int64 ``` + Example: ```go @@ -619,11 +734,16 @@ import ( func main() { now := time.Now() nightTime := datetime.GetNightTimestamp() - fmt.Println(nightTime) // 1643385599 + + fmt.Println(nightTime) + + // Output: + // 1643385599 } ``` ### FormatTimeToStr +

Format time to string, `format` param specification see note 1.

Signature: @@ -631,6 +751,7 @@ func main() { ```go func FormatTimeToStr(t time.Time, format string) string ``` + Example: ```go @@ -643,14 +764,25 @@ import ( ) func main() { - now := time.Now() - timeStr := datetime.FormatTimeToStr(now, "yyyy/mm/dd hh:mm:ss") - fmt.Println(timeStr) //2022/01/28 16:07:44 + t, _ := time.Parse("2006-01-02 15:04:05", "2021-01-02 16:04:08") + + result1 := datetime.FormatTimeToStr(t, "yyyy-mm-dd hh:mm:ss") + result2 := datetime.FormatTimeToStr(t, "yyyy-mm-dd") + result3 := datetime.FormatTimeToStr(t, "dd-mm-yy hh:mm:ss") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // 2021-01-02 16:04:08 + // 2021-01-02 + // 02-01-21 16:04:08 } ``` - ### FormatStrToTime +

Format string to time, `format` param specification see note 1.

Signature: @@ -658,6 +790,7 @@ func main() { ```go func FormatStrToTime(str, format string) (time.Time, error) ``` + Example: ```go @@ -669,24 +802,34 @@ import ( ) func main() { - time := datetime.FormatStrToTime("2006-01-02 15:04:05", "yyyy/mm/dd hh:mm:ss") - fmt.Println(time) + result1, _ := datetime.FormatStrToTime("2021-01-02 16:04:08", "yyyy-mm-dd hh:mm:ss") + result2, _ := datetime.FormatStrToTime("2021-01-02", "yyyy-mm-dd") + result3, _ := datetime.FormatStrToTime("02-01-21 16:04:08", "dd-mm-yy hh:mm:ss") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // 2021-01-02 16:04:08 +0000 UTC + // 2021-01-02 00:00:00 +0000 UTC + // 2021-01-02 16:04:08 +0000 UTC } ``` - - ### NewUnixNow +

Return unix timestamp of current time

Signature: ```go type theTime struct { - unix int64 + unix int64 } func NewUnixNow() *theTime ``` + Example: ```go @@ -699,12 +842,15 @@ import ( func main() { tm := datetime.NewUnixNow() - fmt.Println(tm) //&{1647597438} + fmt.Println(tm) + + // Output: + // &{1647597438} } ``` - ### NewUnix +

Return unix timestamp of specified int64 value.

Signature: @@ -715,6 +861,7 @@ type theTime struct { } func NewUnix(unix int64) *theTime ``` + Example: ```go @@ -727,13 +874,15 @@ import ( func main() { tm := datetime.NewUnix(1647597438) - fmt.Println(tm) //&{1647597438} + fmt.Println(tm) + + // Output: + // &{1647597438} } ``` - - ### NewFormat +

Return unix timestamp of specified time string, t should be "yyyy-mm-dd hh:mm:ss".

Signature: @@ -744,6 +893,7 @@ type theTime struct { } func NewFormat(t string) (*theTime, error) ``` + Example: ```go @@ -756,14 +906,15 @@ import ( func main() { tm, err := datetime.NewFormat("2022-03-18 17:04:05") - fmt.Println(tm) //&{1647594245} + fmt.Println(tm) + + // Output: + // &{1647594245} } ``` - - - ### NewISO8601 +

Return unix timestamp of specified iso8601 time string.

Signature: @@ -774,6 +925,7 @@ type theTime struct { } func NewISO8601(iso8601 string) (*theTime, error) ``` + Example: ```go @@ -786,13 +938,15 @@ import ( func main() { tm, err := datetime.NewISO8601("2006-01-02T15:04:05.999Z") - fmt.Println(tm) //&{1136214245} + fmt.Println(tm) + + // Output: + // &{1136214245} } ``` - - ### ToUnix +

Return unix timestamp.

Signature: @@ -800,6 +954,7 @@ func main() { ```go func (t *theTime) ToUnix() int64 ``` + Example: ```go @@ -812,13 +967,15 @@ import ( func main() { tm := datetime.NewUnixNow() - fmt.Println(tm.ToUnix()) //1647597438 + fmt.Println(tm.ToUnix()) + + // Output: + // 1647597438 } ``` - - ### ToFormat +

Return time string 'yyyy-mm-dd hh:mm:ss'.

Signature: @@ -826,6 +983,7 @@ func main() { ```go func (t *theTime) ToFormat() string ``` + Example: ```go @@ -838,13 +996,15 @@ import ( func main() { tm, _ := datetime.NewFormat("2022-03-18 17:04:05") - fmt.Println(tm.ToFormat()) //"2022-03-18 17:04:05" + fmt.Println(tm.ToFormat()) + + // Output: + // 2022-03-18 17:04:05 } ``` - - ### ToFormatForTpl +

Return the time string which format is specified tpl.

Signature: @@ -852,6 +1012,7 @@ func main() { ```go func (t *theTime) ToFormatForTpl(tpl string) string ``` + Example: ```go @@ -865,12 +1026,15 @@ import ( func main() { tm, _ := datetime.NewFormat("2022-03-18 17:04:05") ts := tm.ToFormatForTpl("2006/01/02 15:04:05") - fmt.Println(ts) //"2022/03/18 17:04:05" + fmt.Println(ts) + + // Output: + // 2022/03/18 17:04:05 } ``` - ### ToIso8601 +

Return iso8601 time string.

Signature: @@ -878,6 +1042,7 @@ func main() { ```go func (t *theTime) ToIso8601() string ``` + Example: ```go @@ -891,6 +1056,9 @@ import ( func main() { tm, _ := datetime.NewISO8601("2006-01-02T15:04:05.999Z") ts := tm.ToIso8601() - fmt.Println(ts) //"2006-01-02T23:04:05+08:00" + fmt.Println(ts) + + // Output: + // 2006-01-02T23:04:05+08:00 } -``` \ No newline at end of file +``` diff --git a/docs/datetime_zh-CN.md b/docs/datetime_zh-CN.md index 918d7f0..8158425 100644 --- a/docs/datetime_zh-CN.md +++ b/docs/datetime_zh-CN.md @@ -77,14 +77,16 @@ import ( ### AddDay -

将日期加/减天数

-函数签名: +

将日期加/减天数。

+ +Signature: ```go func AddDay(t time.Time, day int64) time.Time ``` -例子: + +Example: ```go package main @@ -97,23 +99,33 @@ import ( func main() { now := time.Now() - after2Days := datetime.AddDay(now, 2) - before2Days := datetime.AddDay(now, -2) - fmt.Println(after2Days, before2Days) + tomorrow := datetime.AddDay(now, 1) + diff1 := tomorrow.Sub(now) + + yesterday := datetime.AddDay(now, -1) + diff2 := yesterday.Sub(now) + + fmt.Println(diff1) + fmt.Println(diff2) + + // Output: + // 24h0m0s + // -24h0m0s } ``` - ### AddHour -

将日期加/减小时数

-函数签名: +

将日期加/减小时数。

+ +Signature: ```go func AddHour(t time.Time, hour int64) time.Time ``` -例子: + +Example: ```go package main @@ -126,22 +138,33 @@ import ( func main() { now := time.Now() - after2Hours := datetime.AddHour(now, 2) - before2Hours := datetime.AddHour(now, -2) - fmt.Println(after2Hours, after2Hours) + after2Hours := datetime.AddHour(now, 2) + diff1 := after2Hours.Sub(now) + + before2Hours := datetime.AddHour(now, -2) + diff2 := before2Hours.Sub(now) + + fmt.Println(diff1) + fmt.Println(diff2) + + // Output: + // 2h0m0s + // -2h0m0s } ``` ### AddMinute -

将日期加/减分钟数

-函数签名: +

将日期加/减分钟数。

+ +Signature: ```go func AddMinute(t time.Time, minute int64) time.Time ``` -例子: + +Example: ```go package main @@ -154,22 +177,33 @@ import ( func main() { now := time.Now() - after2Minute := datetime.AddMinute(now, 2) - before2Minute := datetime.AddMinute(now, -2) - fmt.Println(after2Minute, before2Minute) + after2Minutes := datetime.AddMinute(now, 2) + diff1 := after2Minutes.Sub(now) + + before2Minutes := datetime.AddMinute(now, -2) + diff2 := before2Minutes.Sub(now) + + fmt.Println(diff1) + fmt.Println(diff2) + + // Output: + // 2m0s + // -2m0s } ``` ### BeginOfMinute -

返回指定时间的分钟开始时间

-函数签名: +

返回指定时间的分钟开始时间。

+ +Signature: ```go func BeginOfMinute(t time.Time) time.Time ``` -例子: + +Example: ```go package main @@ -181,21 +215,27 @@ import ( ) func main() { - td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local) - bm := datetime.BeginOfMinute(td) - fmt.Println(bm) //2022-02-15 15:48:00 +0800 CST + input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC) + result := datetime.BeginOfMinute(input) + + fmt.Println(result) + + // Output: + // 2023-01-08 18:50:00 +0000 UTC } ``` ### BeginOfHour -

返回指定时间的小时开始时间

-函数签名: +

返回指定时间的小时开始时间。

+ +Signature: ```go func BeginOfHour(t time.Time) time.Time ``` -例子: + +Example: ```go package main @@ -207,21 +247,27 @@ import ( ) func main() { - td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local) - bm := datetime.BeginOfHour(td) - fmt.Println(bm) //2022-02-15 15:00:00 +0800 CST + input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC) + result := datetime.BeginOfHour(input) + + fmt.Println(result) + + // Output: + // 2023-01-08 18:00:00 +0000 UTC } ``` ### BeginOfDay -

返回指定时间的当天开始时间

-函数签名: +

返回指定时间的当天开始时间。

+ +Signature: ```go func BeginOfDay(t time.Time) time.Time ``` -例子: + +Example: ```go package main @@ -233,23 +279,27 @@ import ( ) func main() { - td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local) - bm := datetime.BeginOfDay(td) - fmt.Println(bm) //2022-02-15 00:00:00 +0800 CST + input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC) + result := datetime.BeginOfDay(input) + + fmt.Println(result) + + // Output: + // 2023-01-08 00:00:00 +0000 UTC } ``` - - ### BeginOfWeek -

返回指定时间的每周开始时间,默认开始时间星期日

-函数签名: +

返回指定时间的每周开始时间,默认开始时间星期日。

+ +Signature: ```go func BeginOfWeek(t time.Time, beginFrom ...time.Weekday) time.Time ``` -例子: + +Example: ```go package main @@ -261,23 +311,27 @@ import ( ) func main() { - td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local) - bm := datetime.BeginOfWeek(td) - fmt.Println(bm) //2022-02-13 00:00:00 +0800 CST + input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC) + result := datetime.BeginOfWeek(input) + + fmt.Println(result) + + // Output: + // 2023-01-08 00:00:00 +0000 UTC } ``` - - ### BeginOfMonth -

返回指定时间的当月开始时间

-函数签名: +

返回指定时间的当月开始时间。

+ +Signature: ```go func BeginOfMonth(t time.Time) time.Time ``` -例子: + +Example: ```go package main @@ -289,22 +343,27 @@ import ( ) func main() { - td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local) - bm := datetime.BeginOfMonth(td) - fmt.Println(bm) //2022-02-01 00:00:00 +0800 CST + input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC) + result := datetime.BeginOfMonth(input) + + fmt.Println(result) + + // Output: + // 2023-01-01 00:00:00 +0000 UTC } ``` - ### BeginOfYear +

返回指定时间的当年开始时间

-函数签名: +Signature: ```go func BeginOfYear(t time.Time) time.Time ``` -例子: + +Example: ```go package main @@ -316,23 +375,27 @@ import ( ) func main() { - td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local) - bm := datetime.BeginOfYear(td) - fmt.Println(bm) //2022-01-01 00:00:00 +0800 CST + input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC) + result := datetime.BeginOfYear(input) + + fmt.Println(result) + + // Output: + // 2023-01-01 00:00:00 +0000 UTC } ``` - - ### EndOfMinute -

返回指定时间的分钟结束时间

-函数签名: +

返回指定时间的分钟结束时间。

+ +Signature: ```go func EndOfMinute(t time.Time) time.Time ``` -例子: + +Example: ```go package main @@ -344,21 +407,27 @@ import ( ) func main() { - td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local) - bm := datetime.EndOfMinute(td) - fmt.Println(bm) //2022-02-15 15:48:59.999999999 +0800 CST + input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC) + result := datetime.EndOfMinute(input) + + fmt.Println(result) + + // Output: + // 2023-01-08 18:50:59.999999999 +0000 UTC } ``` ### EndOfHour -

返回指定时间的小时结束时间

-函数签名: +

返回指定时间的小时结束时间。

+ +Signature: ```go func EndOfHour(t time.Time) time.Time ``` -例子: + +Example: ```go package main @@ -370,21 +439,27 @@ import ( ) func main() { - td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local) - bm := datetime.EndOfHour(td) - fmt.Println(bm) //2022-02-15 15:59:59.999999999 +0800 CST + input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC) + result := datetime.EndOfHour(input) + + fmt.Println(result) + + // Output: + // 2023-01-08 18:59:59.999999999 +0000 UTC } ``` ### EndOfDay -

返回指定时间的当天结束时间

-函数签名: +

返回指定时间的当天结束时间。

+ +Signature: ```go func EndOfDay(t time.Time) time.Time ``` -例子: + +Example: ```go package main @@ -396,23 +471,27 @@ import ( ) func main() { - td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local) - bm := datetime.EndOfDay(td) - fmt.Println(bm) //2022-02-15 23:59:59.999999999 +0800 CST + input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC) + result := datetime.EndOfDay(input) + + fmt.Println(result) + + // Output: + // 2023-01-08 23:59:59.999999999 +0000 UTC } ``` - - ### EndOfWeek -

返回指定时间的星期结束时间,默认结束时间星期六

-函数签名: +

返回指定时间的星期结束时间,默认结束时间星期六。

+ +Signature: ```go func EndOfWeek(t time.Time, endWith ...time.Weekday) time.Time ``` -例子: + +Example: ```go package main @@ -424,23 +503,27 @@ import ( ) func main() { - td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local) - bm := datetime.EndOfWeek(td) - fmt.Println(bm) //2022-02-19 23:59:59.999999999 +0800 CST + input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC) + result := datetime.EndOfWeek(input) + + fmt.Println(result) + + // Output: + // 2023-01-14 23:59:59.999999999 +0000 UTC } ``` - - ### EndOfMonth -

返回指定时间的月份结束时间

-函数签名: +

返回指定时间的当月结束时间。

+ +Signature: ```go func EndOfMonth(t time.Time) time.Time ``` -例子: + +Example: ```go package main @@ -452,22 +535,27 @@ import ( ) func main() { - td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local) - bm := datetime.EndOfMonth(td) - fmt.Println(bm) //2022-02-28 23:59:59.999999999 +0800 CST + input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC) + result := datetime.EndOfMonth(input) + + fmt.Println(result) + + // Output: + // 2023-01-31 23:59:59.999999999 +0000 UTC } ``` - ### EndOfYear -

返回指定时间的年份结束时间

-函数签名: +

返回指定时间的当年结束时间。

+ +Signature: ```go func EndOfYear(t time.Time) time.Time ``` -例子: + +Example: ```go package main @@ -479,22 +567,27 @@ import ( ) func main() { - td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local) - bm := datetime.EndOfYear(td) - fmt.Println(bm) //2022-12-31 23:59:59.999999999 +0800 CST + input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC) + result := datetime.EndOfYear(input) + + fmt.Println(result) + + // Output: + // 2023-12-31 23:59:59.999999999 +0000 UTC } ``` - ### GetNowDate -

获取当天日期,返回格式:yyyy-mm-dd

-函数签名: +

获取当天日期,返回格式:yyyy-mm-dd。

+ +Signature: ```go func GetNowDate() string ``` -例子: + +Example: ```go package main @@ -507,21 +600,26 @@ import ( func main() { now := time.Now() - currentDate := datetime.GetNowDate() - fmt.Println(currentDate) // 2022-01-28 + currentDate := datetime.GetNowDate() + + fmt.Println(currentDate) + + // Output: + // 2022-01-28 } ``` - ### GetNowTime +

获取当时时间,返回格式:hh:mm:ss

-函数签名: +Signature: ```go func GetNowTime() string ``` -例子: + +Example: ```go package main @@ -535,20 +633,25 @@ import ( func main() { now := time.Now() currentTime := datetime.GetNowTime() - fmt.Println(currentDate) // 15:57:33 + + fmt.Println(currentTime) // 15:57:33 + + // Output: + // 15:57:33 } ``` - ### GetNowDateTime -

获取当时日期和时间,返回格式:yyyy-mm-dd hh:mm:ss

-函数签名: +

获取当时日期和时间,返回格式:yyyy-mm-dd hh:mm:ss。

+ +Signature: ```go func GetNowDateTime() string ``` -例子: + +Example: ```go package main @@ -562,20 +665,25 @@ import ( func main() { now := time.Now() current := datetime.GetNowDateTime() - fmt.Println(current) // 2022-01-28 15:59:33 + + fmt.Println(current) + + // Output: + // 2022-01-28 15:59:33 } ``` - ### GetZeroHourTimestamp -

获取零时时间戳(timestamp of 00:00)

-函数签名: +

获取零点时间戳(timestamp of 00:00)

+ +Signature: ```go func GetZeroHourTimestamp() int64 ``` -例子: + +Example: ```go package main @@ -589,20 +697,25 @@ import ( func main() { now := time.Now() zeroTime := datetime.GetZeroHourTimestamp() - fmt.Println(zeroTime) // 1643299200 + + fmt.Println(zeroTime) + + // Output: + // 1643299200 } ``` - ### GetNightTimestamp -

获取午夜时间戳(timestamp of 23:59)

-函数签名: +

获取午夜时间戳(timestamp of 23:59)。

+ +Signature: ```go func GetNightTimestamp() int64 ``` -例子: + +Example: ```go package main @@ -616,19 +729,25 @@ import ( func main() { now := time.Now() nightTime := datetime.GetNightTimestamp() - fmt.Println(nightTime) // 1643385599 + + fmt.Println(nightTime) + + // Output: + // 1643385599 } ``` ### FormatTimeToStr -

将日期格式化成字符串,`format` 参数格式参考注1

-函数签名: +

将日期格式化成字符串,`format` 参数格式参考注1。

+ +Signature: ```go func FormatTimeToStr(t time.Time, format string) string ``` -例子: + +Example: ```go package main @@ -640,22 +759,34 @@ import ( ) func main() { - now := time.Now() - timeStr := datetime.FormatTimeToStr(now, "yyyy/mm/dd hh:mm:ss") - fmt.Println(timeStr) //2022/01/28 16:07:44 + t, _ := time.Parse("2006-01-02 15:04:05", "2021-01-02 16:04:08") + + result1 := datetime.FormatTimeToStr(t, "yyyy-mm-dd hh:mm:ss") + result2 := datetime.FormatTimeToStr(t, "yyyy-mm-dd") + result3 := datetime.FormatTimeToStr(t, "dd-mm-yy hh:mm:ss") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // 2021-01-02 16:04:08 + // 2021-01-02 + // 02-01-21 16:04:08 } ``` - ### FormatStrToTime -

将字符串格式化成时间,`format` 参数格式参考注1

-函数签名: +

将字符串格式化成时间,`format` 参数格式参考注1。

+ +Signature: ```go func FormatStrToTime(str, format string) (time.Time, error) ``` -例子: + +Example: ```go package main @@ -666,16 +797,26 @@ import ( ) func main() { - time := datetime.FormatStrToTime("2006-01-02 15:04:05", "yyyy/mm/dd hh:mm:ss") - fmt.Println(time) + result1, _ := datetime.FormatStrToTime("2021-01-02 16:04:08", "yyyy-mm-dd hh:mm:ss") + result2, _ := datetime.FormatStrToTime("2021-01-02", "yyyy-mm-dd") + result3, _ := datetime.FormatStrToTime("02-01-21 16:04:08", "dd-mm-yy hh:mm:ss") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // 2021-01-02 16:04:08 +0000 UTC + // 2021-01-02 00:00:00 +0000 UTC + // 2021-01-02 16:04:08 +0000 UTC } ``` - ### NewUnixNow -

创建一个当前时间的unix时间戳

-函数签名: +

创建一个当前时间的unix时间戳。

+ +Signature: ```go type theTime struct { @@ -683,7 +824,8 @@ type theTime struct { } func NewUnixNow() *theTime ``` -例子: + +Example: ```go package main @@ -695,15 +837,18 @@ import ( func main() { tm := datetime.NewUnixNow() - fmt.Println(tm) //&{1647597438} + fmt.Println(tm) + + // Output: + // &{1647597438} } ``` - ### NewUnix -

创建一个unix时间戳

-函数签名: +

创建一个unix时间戳。

+ +Signature: ```go type theTime struct { @@ -711,7 +856,8 @@ type theTime struct { } func NewUnix(unix int64) *theTime ``` -例子: + +Example: ```go package main @@ -723,16 +869,18 @@ import ( func main() { tm := datetime.NewUnix(1647597438) - fmt.Println(tm) //&{1647597438} + fmt.Println(tm) + + // Output: + // &{1647597438} } ``` - - ### NewFormat -

创建一个yyyy-mm-dd hh:mm:ss格式时间字符串的unix时间戳

-函数签名: +

创建一个yyyy-mm-dd hh:mm:ss格式时间字符串的unix时间戳。

+ +Signature: ```go type theTime struct { @@ -740,7 +888,8 @@ type theTime struct { } func NewFormat(t string) (*theTime, error) ``` -例子: + +Example: ```go package main @@ -752,17 +901,18 @@ import ( func main() { tm, err := datetime.NewFormat("2022-03-18 17:04:05") - fmt.Println(tm) //&{1647594245} + fmt.Println(tm) + + // Output: + // &{1647594245} } ``` - - - ### NewISO8601 -

创建一个iso8601格式时间字符串的unix时间戳

-函数签名: +

创建一个iso8601格式时间字符串的unix时间戳。

+ +Signature: ```go type theTime struct { @@ -770,7 +920,8 @@ type theTime struct { } func NewISO8601(iso8601 string) (*theTime, error) ``` -例子: + +Example: ```go package main @@ -782,21 +933,24 @@ import ( func main() { tm, err := datetime.NewISO8601("2006-01-02T15:04:05.999Z") - fmt.Println(tm) //&{1136214245} + fmt.Println(tm) + + // Output: + // &{1136214245} } ``` - - ### ToUnix -

返回unix时间戳

-函数签名: +

返回unix时间戳。

+ +Signature: ```go func (t *theTime) ToUnix() int64 ``` -例子: + +Example: ```go package main @@ -808,21 +962,24 @@ import ( func main() { tm := datetime.NewUnixNow() - fmt.Println(tm.ToUnix()) //1647597438 + fmt.Println(tm.ToUnix()) + + // Output: + // 1647597438 } ``` - - ### ToFormat -

返回格式'yyyy-mm-dd hh:mm:ss'的日期字符串

-函数签名: +

返回格式'yyyy-mm-dd hh:mm:ss'的日期字符串。

+ +Signature: ```go func (t *theTime) ToFormat() string ``` -例子: + +Example: ```go package main @@ -834,21 +991,24 @@ import ( func main() { tm, _ := datetime.NewFormat("2022-03-18 17:04:05") - fmt.Println(tm.ToFormat()) //"2022-03-18 17:04:05" + fmt.Println(tm.ToFormat()) + + // Output: + // 2022-03-18 17:04:05 } ``` - - ### ToFormatForTpl -

返回tpl格式指定的日期字符串

-函数签名: +

返回tpl格式指定的日期字符串。

+ +Signature: ```go func (t *theTime) ToFormatForTpl(tpl string) string ``` -例子: + +Example: ```go package main @@ -861,20 +1021,24 @@ import ( func main() { tm, _ := datetime.NewFormat("2022-03-18 17:04:05") ts := tm.ToFormatForTpl("2006/01/02 15:04:05") - fmt.Println(ts) //"2022/03/18 17:04:05" + fmt.Println(ts) + + // Output: + // 2022/03/18 17:04:05 } ``` - ### ToIso8601 -

返回iso8601日期字符串

-函数签名: +

返回iso8601日期字符串。

+ +Signature: ```go func (t *theTime) ToIso8601() string ``` -例子: + +Example: ```go package main @@ -887,6 +1051,9 @@ import ( func main() { tm, _ := datetime.NewISO8601("2006-01-02T15:04:05.999Z") ts := tm.ToIso8601() - fmt.Println(ts) //"2006-01-02T23:04:05+08:00" + fmt.Println(ts) + + // Output: + // 2006-01-02T23:04:05+08:00 } -``` +``` \ No newline at end of file diff --git a/docs/fileutil.md b/docs/fileutil.md index cd5428c..22f4f81 100644 --- a/docs/fileutil.md +++ b/docs/fileutil.md @@ -1,15 +1,17 @@ # Fileutil + Package fileutil implements some basic functions for file operations.
## Source: -- [https://github.com/duke-git/lancet/blob/main/fileutil/file.go](https://github.com/duke-git/lancet/blob/main/fileutil/file.go) +- [https://github.com/duke-git/lancet/blob/main/fileutil/file.go](https://github.com/duke-git/lancet/blob/main/fileutil/file.go)
## Usage: + ```go import ( "github.com/duke-git/lancet/v2/fileutil" @@ -19,30 +21,30 @@ import (
## Index -- [ClearFile](#ClearFile) -- [CreateFile](#CreateFile) -- [CreateDir](#CreateDir) -- [CopyFile](#CopyFile) -- [FileMode](#FileMode) -- [MiMeType](#MiMeType) -- [IsExist](#IsExist) -- [IsLink](#IsLink) -- [IsDir](#IsDir) -- [ListFileNames](#ListFileNames) -- [RemoveFile](#RemoveFile) -- [ReadFileToString](#ReadFileToString) -- [ReadFileByLine](#ReadFileByLine) -- [Zip](#Zip) -- [UnZip](#UnZip) -- [UnZip](#UnZip) + +- [ClearFile](#ClearFile) +- [CreateFile](#CreateFile) +- [CreateDir](#CreateDir) +- [CopyFile](#CopyFile) +- [FileMode](#FileMode) +- [MiMeType](#MiMeType) +- [IsExist](#IsExist) +- [IsLink](#IsLink) +- [IsDir](#IsDir) +- [ListFileNames](#ListFileNames) +- [RemoveFile](#RemoveFile) +- [ReadFileToString](#ReadFileToString) +- [ReadFileByLine](#ReadFileByLine) +- [Zip](#Zip) +- [UnZip](#UnZip) +- [UnZip](#UnZip)
## Documentation - - ### ClearFile +

Clear the file content, write empty string to the file.

Signature: @@ -50,6 +52,7 @@ import ( ```go func ClearFile(path string) error ``` + Example: ```go @@ -69,6 +72,7 @@ func main() { ``` ### CreateFile +

Create file in path. return true if create succeed.

Signature: @@ -76,6 +80,7 @@ func main() { ```go func CreateFile(path string) bool ``` + Example: ```go @@ -92,9 +97,8 @@ func main() { } ``` - - ### CreateDir +

Create directory in absolute path. param `absPath` like /a/, /a/b/.

Signature: @@ -102,6 +106,7 @@ func main() { ```go func CreateDir(absPath string) error ``` + Example: ```go @@ -118,8 +123,8 @@ func main() { } ``` - ### CopyFile +

Copy src file to dest file. If dest file exist will overwrite it.

Signature: @@ -127,6 +132,7 @@ func main() { ```go func CopyFile(srcFilePath string, dstFilePath string) error ``` + Example: ```go @@ -145,9 +151,8 @@ func main() { } ``` - - ### FileMode +

Return file mode infomation.

Signature: @@ -155,6 +160,7 @@ func main() { ```go func FileMode(path string) (fs.FileMode, error) ``` + Example: ```go @@ -174,9 +180,8 @@ func main() { } ``` - - ### MiMeType +

Get file mime type, 'file' param's type should be string or *os.File.

Signature: @@ -184,6 +189,7 @@ func main() { ```go func MiMeType(file any) string ``` + Example: ```go @@ -205,10 +211,8 @@ func main() { } ``` - - - ### IsExist +

Checks if a file or directory exists.

Signature: @@ -216,6 +220,7 @@ func main() { ```go func IsExist(path string) bool ``` + Example: ```go @@ -233,9 +238,8 @@ func main() { } ``` - - ### IsLink +

Checks if a file is symbol link or not.

Signature: @@ -243,6 +247,7 @@ func main() { ```go func IsLink(path string) bool ``` + Example: ```go @@ -259,16 +264,16 @@ func main() { } ``` - - ### IsDir +

Checks if the path is directy or not.

Signature: ```go -func IsDir(path string) bool +func IsDir(path string) bool ``` + Example: ```go @@ -288,9 +293,8 @@ func main() { } ``` - - ### ListFileNames +

List all file names in given path.

Signature: @@ -298,6 +302,7 @@ func main() { ```go func ListFileNames(path string) ([]string, error) ``` + Example: ```go @@ -314,9 +319,8 @@ func main() { } ``` - - ### RemoveFile +

Remove the file of path.

Signature: @@ -324,6 +328,7 @@ func main() { ```go func RemoveFile(path string) error ``` + Example: ```go @@ -342,8 +347,8 @@ func main() { } ``` - ### ReadFileToString +

Return string of file content.

Signature: @@ -351,6 +356,7 @@ func main() { ```go func ReadFileToString(path string) (string, error) ``` + Example: ```go @@ -374,9 +380,8 @@ func main() { } ``` - - ### ReadFileByLine +

Read file line by line, and return slice of lines

Signature: @@ -384,6 +389,7 @@ func main() { ```go func ReadFileByLine(path string)([]string, error) ``` + Example: ```go @@ -408,9 +414,8 @@ func main() { } ``` - - ### Zip +

Create a zip file of fpath, fpath could be a file or a directory.

Signature: @@ -418,6 +423,7 @@ func main() { ```go func Zip(fpath string, destPath string) error ``` + Example: ```go @@ -436,10 +442,8 @@ func main() { } ``` - - - ### UnZip +

Unzip the file and save it to dest path.

Signature: @@ -447,6 +451,7 @@ func main() { ```go func UnZip(zipFile string, destPath string) error ``` + Example: ```go @@ -464,8 +469,3 @@ func main() { } } ``` - - - - - diff --git a/docs/fileutil_zh-CN.md b/docs/fileutil_zh-CN.md index 39d04b4..d283381 100644 --- a/docs/fileutil_zh-CN.md +++ b/docs/fileutil_zh-CN.md @@ -1,15 +1,17 @@ # Fileutil -fileutil包支持文件基本操作。 + +fileutil 包支持文件基本操作。
## 源码: -- [https://github.com/duke-git/lancet/blob/main/fileutil/file.go](https://github.com/duke-git/lancet/blob/main/fileutil/file.go) +- [https://github.com/duke-git/lancet/blob/main/fileutil/file.go](https://github.com/duke-git/lancet/blob/main/fileutil/file.go)
## 用法: + ```go import ( "github.com/duke-git/lancet/v2/fileutil" @@ -19,29 +21,29 @@ import (
## 目录 -- [ClearFile](#ClearFile) -- [CreateFile](#CreateFile) -- [CreateDir](#CreateDir) -- [CopyFile](#CopyFile) -- [FileMode](#FileMode) -- [MiMeType](#MiMeType) -- [IsExist](#IsExist) -- [IsLink](#IsLink) -- [IsDir](#IsDir) -- [ListFileNames](#ListFileNames) -- [RemoveFile](#RemoveFile) -- [ReadFileToString](#ReadFileToString) -- [ReadFileByLine](#ReadFileByLine) -- [Zip](#Zip) -- [UnZip](#UnZip) + +- [ClearFile](#ClearFile) +- [CreateFile](#CreateFile) +- [CreateDir](#CreateDir) +- [CopyFile](#CopyFile) +- [FileMode](#FileMode) +- [MiMeType](#MiMeType) +- [IsExist](#IsExist) +- [IsLink](#IsLink) +- [IsDir](#IsDir) +- [ListFileNames](#ListFileNames) +- [RemoveFile](#RemoveFile) +- [ReadFileToString](#ReadFileToString) +- [ReadFileByLine](#ReadFileByLine) +- [Zip](#Zip) +- [UnZip](#UnZip)
## 文档 - - ### ClearFile +

清空文件内容

函数签名: @@ -49,6 +51,7 @@ import ( ```go func ClearFile(path string) error ``` + 例子: ```go @@ -68,6 +71,7 @@ func main() { ``` ### CreateFile +

创建文件,创建成功返回true, 否则返回false

函数签名: @@ -75,6 +79,7 @@ func main() { ```go func CreateFile(path string) bool ``` + 例子: ```go @@ -91,8 +96,8 @@ func main() { } ``` - ### CreateDir +

使用绝对路径创建嵌套目录,例如/a/, /a/b/

函数签名: @@ -100,6 +105,7 @@ func main() { ```go func CreateDir(absPath string) error ``` + Example: ```go @@ -116,9 +122,8 @@ func main() { } ``` - - ### CopyFile +

拷贝文件,会覆盖原有的文件

函数签名: @@ -126,6 +131,7 @@ func main() { ```go func CopyFile(srcFilePath string, dstFilePath string) error ``` + 例子: ```go @@ -144,9 +150,8 @@ func main() { } ``` - - ### FileMode +

获取文件mode信息

函数签名: @@ -154,6 +159,7 @@ func main() { ```go func FileMode(path string) (fs.FileMode, error) ``` + 例子: ```go @@ -173,9 +179,8 @@ func main() { } ``` - - ### MiMeType +

获取文件mime类型, 'file'参数的类型必须是string或者*os.File

函数签名: @@ -183,6 +188,7 @@ func main() { ```go func MiMeType(file any) string ``` + 例子: ```go @@ -204,10 +210,8 @@ func main() { } ``` - - - ### IsExist +

判断文件或目录是否存在

函数签名: @@ -215,6 +219,7 @@ func main() { ```go func IsExist(path string) bool ``` + 例子: ```go @@ -232,9 +237,8 @@ func main() { } ``` - - ### IsLink +

判断文件是否是符号链接

函数签名: @@ -242,6 +246,7 @@ func main() { ```go func IsLink(path string) bool ``` + 例子: ```go @@ -258,16 +263,16 @@ func main() { } ``` - - ### IsDir +

判断参数是否是目录

函数签名: ```go -func IsDir(path string) bool +func IsDir(path string) bool ``` + 例子: ```go @@ -287,9 +292,8 @@ func main() { } ``` - - ### ListFileNames +

返回目录下所有文件名

函数签名: @@ -297,6 +301,7 @@ func main() { ```go func ListFileNames(path string) ([]string, error) ``` + 例子: ```go @@ -313,9 +318,8 @@ func main() { } ``` - - ### RemoveFile +

删除文件

函数签名: @@ -323,6 +327,7 @@ func main() { ```go func RemoveFile(path string) error ``` + 例子: ```go @@ -341,8 +346,8 @@ func main() { } ``` - ### ReadFileToString +

读取文件内容并返回字符串

函数签名: @@ -350,6 +355,7 @@ func main() { ```go func ReadFileToString(path string) (string, error) ``` + 例子: ```go @@ -373,9 +379,8 @@ func main() { } ``` - - ### ReadFileByLine +

按行读取文件内容,返回字符串切片包含每一行

函数签名: @@ -383,6 +388,7 @@ func main() { ```go func ReadFileByLine(path string)([]string, error) ``` + 例子: ```go @@ -407,9 +413,8 @@ func main() { } ``` - - ### Zip +

zip压缩文件, fpath参数可以是文件或目录

函数签名: @@ -417,6 +422,7 @@ func main() { ```go func Zip(fpath string, destPath string) error ``` + 例子: ```go @@ -435,10 +441,8 @@ func main() { } ``` - - - ### UnZip +

zip解压缩文件并保存在目录中

Signature: @@ -446,6 +450,7 @@ func main() { ```go func UnZip(zipFile string, destPath string) error ``` + 例子: ```go @@ -463,8 +468,3 @@ func main() { } } ``` - - - - - diff --git a/docs/formatter.md b/docs/formatter.md index 147a9a5..d022e02 100644 --- a/docs/formatter.md +++ b/docs/formatter.md @@ -1,15 +1,17 @@ # Formatter + formatter contains some functions for data formatting.
## Source: -- [https://github.com/duke-git/lancet/blob/main/formatter/formatter.go](https://github.com/duke-git/lancet/blob/main/formatter/formatter.go) +- [https://github.com/duke-git/lancet/blob/main/formatter/formatter.go](https://github.com/duke-git/lancet/blob/main/formatter/formatter.go)
## Usage: + ```go import ( "github.com/duke-git/lancet/v2/formatter" @@ -19,15 +21,15 @@ import (
## Index -- [Comma](#Comma) + +- [Comma](#Comma)
## Documentation - - ### Comma +

Add comma to a number value by every 3 numbers from right to left. ahead by symbol char. if value is a invalid number string like "aa", return empty string.

Signature: @@ -35,6 +37,7 @@ import ( ```go func Comma[T constraints.Float | constraints.Integer | string](value T, symbol string) string ``` + Example: ```go @@ -46,7 +49,17 @@ import ( ) func main() { - fmt.Println(formatter.Comma("12345", "")) // "12,345" - fmt.Println(formatter.Comma(12345.67, "¥")) // "¥12,345.67" + result1 := formatter.Comma("123", "") + result2 := formatter.Comma("12345", "$") + result3 := formatter.Comma(1234567, "¥") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // 123 + // $12,345 + // ¥1,234,567 } ``` diff --git a/docs/formatter_zh-CN.md b/docs/formatter_zh-CN.md index 1c47b59..2bbe7a0 100644 --- a/docs/formatter_zh-CN.md +++ b/docs/formatter_zh-CN.md @@ -1,15 +1,17 @@ # Formatter -formatter格式化器包含一些数据格式化处理方法。 + +formatter 格式化器包含一些数据格式化处理方法。
## 源码: -- [https://github.com/duke-git/lancet/blob/main/formatter/formatter.go](https://github.com/duke-git/lancet/blob/main/formatter/formatter.go) +- [https://github.com/duke-git/lancet/blob/main/formatter/formatter.go](https://github.com/duke-git/lancet/blob/main/formatter/formatter.go)
## 用法: + ```go import ( "github.com/duke-git/lancet/v2/formatter" @@ -19,15 +21,15 @@ import (
## 目录 -- [Comma](#Comma) + +- [Comma](#Comma)
## 文档 - - ### Comma +

用逗号每隔3位分割数字/字符串,支持前缀添加符号。参数value必须是数字或者可以转为数字的字符串, 否则返回空字符串

函数签名: @@ -35,6 +37,7 @@ import ( ```go func Comma[T constraints.Float | constraints.Integer | string](value T, symbol string) string ``` + 例子: ```go @@ -46,7 +49,17 @@ import ( ) func main() { - fmt.Println(formatter.Comma("12345", "")) // "12,345" - fmt.Println(formatter.Comma(12345.67, "¥")) // "¥12,345.67" + result1 := formatter.Comma("123", "") + result2 := formatter.Comma("12345", "$") + result3 := formatter.Comma(1234567, "¥") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // 123 + // $12,345 + // ¥1,234,567 } ``` diff --git a/docs/function.md b/docs/function.md index 4abb9e9..dbefec3 100644 --- a/docs/function.md +++ b/docs/function.md @@ -1,16 +1,18 @@ # Function + Package function can control the flow of function execution and support part of functional programming.
## Source: -- [https://github.com/duke-git/lancet/blob/main/function/function.go](https://github.com/duke-git/lancet/blob/main/function/function.go) -- [https://github.com/duke-git/lancet/blob/main/function/watcher.go](https://github.com/duke-git/lancet/blob/main/function/watcher.go) +- [https://github.com/duke-git/lancet/blob/main/function/function.go](https://github.com/duke-git/lancet/blob/main/function/function.go) +- [https://github.com/duke-git/lancet/blob/main/function/watcher.go](https://github.com/duke-git/lancet/blob/main/function/watcher.go)
## Usage: + ```go import ( "github.com/duke-git/lancet/v2/function" @@ -20,22 +22,22 @@ import (
## Index -- [After](#After) -- [Before](#Before) -- [CurryFn](#CurryFn) -- [Compose](#Compose) -- [Debounced](#Debounced) -- [Delay](#Delay) -- [Pipeline](#Pipeline) -- [Watcher](#Watcher) + +- [After](#After) +- [Before](#Before) +- [CurryFn](#CurryFn) +- [Compose](#Compose) +- [Debounced](#Debounced) +- [Delay](#Delay) +- [Pipeline](#Pipeline) +- [Watcher](#Watcher)
## Documentation - - ### After +

Creates a function that invokes given func once it's called n or more times.

Signature: @@ -43,6 +45,7 @@ import ( ```go func After(n int, fn any) func(args ...any) []reflect.Value ``` + Example: ```go @@ -54,33 +57,18 @@ import ( ) func main() { - arr := []string{"a", "b"} - f := function.After(len(arr), func(i int) int { - fmt.Println("last print") - return i - }) + fn := function.After(2, func() { + fmt.Println("hello") + }) - type cb func(args ...any) []reflect.Value - print := func(i int, s string, fn cb) { - fmt.Printf("arr[%d] is %s \n", i, s) - fn(i) - } + fn() + fn() - fmt.Println("arr is", arr) - for i := 0; i < len(arr); i++ { - print(i, arr[i], f) - } - - //output: - // arr is [a b] - // arr[0] is a - // arr[1] is b - // last print + // Output: + // hello } ``` - - ### Before

creates a function that invokes func once it's called less than n times.

@@ -90,6 +78,7 @@ func main() { ```go func Before(n int, fn any) func(args ...any) []reflect.Value ``` + Example: ```go @@ -102,29 +91,21 @@ import ( ) func main() { - arr := []string{"a", "b", "c", "d", "e"} - f := function.Before(3, func(i int) int { - return i - }) + fn := function.Before(2, func() { + fmt.Println("hello") + }) - var res []int64 - type cb func(args ...any) []reflect.Value - appendStr := func(i int, s string, fn cb) { - v := fn(i) - res = append(res, v[0].Int()) - } + fn() + fn() + fn() + fn() - for i := 0; i < len(arr); i++ { - appendStr(i, arr[i], f) - } - - expected := []int64{0, 1, 2, 2, 2} - fmt.Println(res) // 0, 1, 2, 2, 2 + // Output: + // hello + // hello } ``` - - ### CurryFn

Make curry function.

@@ -135,6 +116,7 @@ func main() { type CurryFn[T any] func(...T) T func (cf CurryFn[T]) New(val T) func(...T) T ``` + Example: ```go @@ -147,22 +129,23 @@ import ( func main() { add := func(a, b int) int { - return a + b - } + return a + b + } - var addCurry CurryFn[int] = func(values ...int) int { - return add(values[0], values[1]) - } - add1 := addCurry.New(1) + var addCurry function.CurryFn[int] = func(values ...int) int { + return add(values[0], values[1]) + } + add1 := addCurry.New(1) - result := add1(2) + result := add1(2) - fmt.Println(result) //3 + fmt.Println(result) + + // Output: + // 3 } ``` - - ### Compose

Compose the function list from right to left, then return the composed function.

@@ -172,6 +155,7 @@ func main() { ```go func Compose[T any](fnList ...func(...T) T) func(...T) T ``` + Example: ```go @@ -183,22 +167,23 @@ import ( ) func main() { - toUpper := func(strs ...string) string { - return strings.ToUpper(strs[0]) - } - toLower := func(strs ...string) string { - return strings.ToLower(strs[0]) - } - transform := Compose(toUpper, toLower) + toUpper := func(strs ...string) string { + return strings.ToUpper(strs[0]) + } + toLower := func(strs ...string) string { + return strings.ToLower(strs[0]) + } + transform := function.Compose(toUpper, toLower) - result := transform("aBCde") + result := transform("aBCde") - fmt.Println(result) //ABCDE + fmt.Println(result) + + // Output: + // ABCDE } ``` - - ### Debounced

Creates a debounced function that delays invoking fn until after wait duration have elapsed since the last time the debounced function was invoked.

@@ -208,6 +193,7 @@ func main() { ```go func Debounced(fn func(), duration time.Duration) func() ``` + Example: ```go @@ -220,27 +206,34 @@ import ( func main() { count := 0 - add := func() { - count++ - } - debouncedAdd := function.Debounced(add, 50*time.Microsecond) - function.debouncedAdd() - function.debouncedAdd() - function.debouncedAdd() - function.debouncedAdd() + add := func() { + count++ + } - time.Sleep(100 * time.Millisecond) - fmt.Println(count) //1 + debouncedAdd := function.Debounced(add, 50*time.Microsecond) - function.debouncedAdd() - time.Sleep(100 * time.Millisecond) - fmt.Println(count) //2 + debouncedAdd() + debouncedAdd() + debouncedAdd() + debouncedAdd() + + time.Sleep(100 * time.Millisecond) + + fmt.Println(count) + + debouncedAdd() + + time.Sleep(100 * time.Millisecond) + + fmt.Println(count) + + // Output: + // 1 + // 2 } ``` - - ### Delay

Invoke function after delayed time.

@@ -250,6 +243,7 @@ func main() { ```go func Delay(delay time.Duration, fn any, args ...any) ``` + Example: ```go @@ -261,15 +255,17 @@ import ( ) func main() { - var print = func(s string) { - fmt.Println(count) //delay 2 seconds - } - function.Delay(2*time.Second, print, "delay 2 seconds") + var print = func(s string) { + fmt.Println(s) + } + + function.Delay(2*time.Second, print, "hello") + + // Output: + // hello } ``` - - ### Schedule

Invoke function every duration time, until close the returned bool chan.

@@ -279,6 +275,7 @@ func main() { ```go func Schedule(d time.Duration, fn any, args ...any) chan bool ``` + Example: ```go @@ -290,20 +287,24 @@ import ( ) func main() { - var res []string - appendStr := func(s string) { - res = append(res, s) - } + count := 0 - stop := function.Schedule(1*time.Second, appendStr, "*") - time.Sleep(5 * time.Second) - close(stop) + increase := func() { + count++ + } - fmt.Println(res) //[* * * * *] + stop := function.Schedule(2*time.Second, increase) + + time.Sleep(2 * time.Second) + close(stop) + + fmt.Println(count) + + // Output: + // 2 } ``` - ### Pipeline

Pipeline takes a list of functions and returns a function whose param will be passed into @@ -314,6 +315,7 @@ the functions one by one.

```go func Pipeline[T any](funcs ...func(T) T) func(T) T ``` + Example: ```go @@ -326,22 +328,26 @@ import ( func main() { addOne := func(x int) int { - return x + 1 - } - double := func(x int) int { - return 2 * x - } - square := func(x int) int { - return x * x - } + return x + 1 + } + double := func(x int) int { + return 2 * x + } + square := func(x int) int { + return x * x + } - fn := Pipeline(addOne, double, square) + fn := function.Pipeline(addOne, double, square) - fmt.Println(fn(2)) //36 + result := fn(2) + + fmt.Println(result) + + // Output: + // 36 } ``` - ### Watcher

Watcher is used for record code excution time. can start/stop/reset the watch timer. get the elapsed time of function execution.

@@ -350,9 +356,9 @@ func main() { ```go type Watcher struct { - startTime int64 - stopTime int64 - excuting bool + startTime int64 + stopTime int64 + excuting bool } func NewWatcher() *Watcher func (w *Watcher) Start() //start the watcher @@ -360,6 +366,7 @@ func (w *Watcher) Stop() //stop the watcher func (w *Watcher) Reset() //reset the watcher func (w *Watcher) GetElapsedTime() time.Duration //get the elapsed time of function execution ``` + Example: ```go @@ -371,31 +378,28 @@ import ( ) func main() { - w := function.NewWatcher() + w := function.NewWatcher() - w.Start() + w.Start() - longRunningTask() + longRunningTask() - fmt.Println(w.excuting) //true + fmt.Println(w.excuting) //true - w.Stop() + w.Stop() - eapsedTime := w.GetElapsedTime().Milliseconds() - - fmt.Println(eapsedTime) + eapsedTime := w.GetElapsedTime().Milliseconds() - w.Reset() + fmt.Println(eapsedTime) + + w.Reset() } func longRunningTask() { - var slice []int64 - for i := 0; i < 10000000; i++ { - slice = append(slice, int64(i)) - } + var slice []int64 + for i := 0; i < 10000000; i++ { + slice = append(slice, int64(i)) + } } ``` - - - diff --git a/docs/function_zh-CN.md b/docs/function_zh-CN.md index ab88076..9972e16 100644 --- a/docs/function_zh-CN.md +++ b/docs/function_zh-CN.md @@ -1,16 +1,18 @@ # Function -function函数包控制函数执行流程,包含部分函数式编程。 + +function 函数包控制函数执行流程,包含部分函数式编程。
## 源码: -- [https://github.com/duke-git/lancet/blob/main/function/function.go](https://github.com/duke-git/lancet/blob/main/function/function.go) -- [https://github.com/duke-git/lancet/blob/main/function/watcher.go](https://github.com/duke-git/lancet/blob/main/function/watcher.go) +- [https://github.com/duke-git/lancet/blob/main/function/function.go](https://github.com/duke-git/lancet/blob/main/function/function.go) +- [https://github.com/duke-git/lancet/blob/main/function/watcher.go](https://github.com/duke-git/lancet/blob/main/function/watcher.go)
## 用法: + ```go import ( "github.com/duke-git/lancet/v2/function" @@ -20,22 +22,22 @@ import (
## 目录 -- [After](#After) -- [Before](#Before) -- [CurryFn](#CurryFn) -- [Compose](#Compose) -- [Debounced](#Debounced) -- [Delay](#Delay) -- [Pipeline](#Pipeline) -- [Watcher](#Watcher) + +- [After](#After) +- [Before](#Before) +- [CurryFn](#CurryFn) +- [Compose](#Compose) +- [Debounced](#Debounced) +- [Delay](#Delay) +- [Pipeline](#Pipeline) +- [Watcher](#Watcher)
## 文档 - - ### After +

创建一个函数,当他被调用n或更多次之后将马上触发fn

函数签名: @@ -43,6 +45,7 @@ import ( ```go func After(n int, fn any) func(args ...any) []reflect.Value ``` + 例子: ```go @@ -54,33 +57,18 @@ import ( ) func main() { - arr := []string{"a", "b"} - f := function.After(len(arr), func(i int) int { - fmt.Println("last print") - return i - }) + fn := function.After(2, func() { + fmt.Println("hello") + }) - type cb func(args ...any) []reflect.Value - print := func(i int, s string, fn cb) { - fmt.Printf("arr[%d] is %s \n", i, s) - fn(i) - } + fn() + fn() - fmt.Println("arr is", arr) - for i := 0; i < len(arr); i++ { - print(i, arr[i], f) - } - - //output: - // arr is [a b] - // arr[0] is a - // arr[1] is b - // last print + // Output: + // hello } ``` - - ### Before

创建一个函数,调用次数不超过n次,之后再调用这个函数,将返回一次最后调用fn的结果

@@ -90,6 +78,7 @@ func main() { ```go func Before(n int, fn any) func(args ...any) []reflect.Value ``` + 例子: ```go @@ -102,28 +91,21 @@ import ( ) func main() { - arr := []string{"a", "b", "c", "d", "e"} - f := function.Before(3, func(i int) int { - return i - }) + fn := function.Before(2, func() { + fmt.Println("hello") + }) - var res []int64 - type cb func(args ...any) []reflect.Value - appendStr := func(i int, s string, fn cb) { - v := fn(i) - res = append(res, v[0].Int()) - } + fn() + fn() + fn() + fn() - for i := 0; i < len(arr); i++ { - appendStr(i, arr[i], f) - } - - fmt.Println(res) // 0, 1, 2, 2, 2 + // Output: + // hello + // hello } ``` - - ### CurryFn

创建柯里化函数

@@ -134,6 +116,7 @@ func main() { type CurryFn[T any] func(...T) T func (cf CurryFn[T]) New(val T) func(...T) T ``` + 例子: ```go @@ -145,23 +128,24 @@ import ( ) func main() { - add := func(a, b int) int { - return a + b - } + add := func(a, b int) int { + return a + b + } - var addCurry CurryFn[int] = func(values ...int) int { - return add(values[0], values[1]) - } - add1 := addCurry.New(1) + var addCurry function.CurryFn[int] = func(values ...int) int { + return add(values[0], values[1]) + } + add1 := addCurry.New(1) - result := add1(2) + result := add1(2) - fmt.Println(result) //3 + fmt.Println(result) + + // Output: + // 3 } ``` - - ### Compose

从右至左组合函数列表fnList,返回组合后的函数

@@ -171,6 +155,7 @@ func main() { ```go func Compose[T any](fnList ...func(...T) T) func(...T) T ``` + 例子: ```go @@ -182,22 +167,23 @@ import ( ) func main() { - toUpper := func(strs ...string) string { - return strings.ToUpper(strs[0]) - } - toLower := func(strs ...string) string { - return strings.ToLower(strs[0]) - } - transform := Compose(toUpper, toLower) + toUpper := func(strs ...string) string { + return strings.ToUpper(strs[0]) + } + toLower := func(strs ...string) string { + return strings.ToLower(strs[0]) + } + transform := function.Compose(toUpper, toLower) - result := transform("aBCde") + result := transform("aBCde") - fmt.Println(result) //ABCDE + fmt.Println(result) + + // Output: + // ABCDE } ``` - - ### Debounced

创建一个debounced函数,该函数延迟调用fn直到自上次调用debounced函数后等待持续时间过去。

@@ -207,6 +193,7 @@ func main() { ```go func Debounced(fn func(), duration time.Duration) func() ``` + 例子: ```go @@ -218,28 +205,35 @@ import ( ) func main() { - count := 0 - add := func() { - count++ - } + count := 0 + + add := func() { + count++ + } - debouncedAdd := function.Debounced(add, 50*time.Microsecond) - function.debouncedAdd() - function.debouncedAdd() - function.debouncedAdd() - function.debouncedAdd() + debouncedAdd := function.Debounced(add, 50*time.Microsecond) - time.Sleep(100 * time.Millisecond) - fmt.Println(count) //1 + debouncedAdd() + debouncedAdd() + debouncedAdd() + debouncedAdd() - function.debouncedAdd() - time.Sleep(100 * time.Millisecond) - fmt.Println(count) //2 + time.Sleep(100 * time.Millisecond) + + fmt.Println(count) + + debouncedAdd() + + time.Sleep(100 * time.Millisecond) + + fmt.Println(count) + + // Output: + // 1 + // 2 } ``` - - ### Delay

延迟delay时间后调用函数

@@ -249,6 +243,7 @@ func main() { ```go func Delay(delay time.Duration, fn any, args ...any) ``` + 例子: ```go @@ -260,15 +255,17 @@ import ( ) func main() { - var print = func(s string) { - fmt.Println(count) //test delay - } - function.Delay(2*time.Second, print, "test delay") + var print = func(s string) { + fmt.Println(s) + } + + function.Delay(2*time.Second, print, "hello") + + // Output: + // hello } ``` - - ### Schedule

每次持续时间调用函数,直到关闭返回的 bool chan

@@ -278,6 +275,7 @@ func main() { ```go func Schedule(d time.Duration, fn any, args ...any) chan bool ``` + 例子: ```go @@ -289,21 +287,24 @@ import ( ) func main() { - var res []string - appendStr := func(s string) { - res = append(res, s) - } + count := 0 - stop := function.Schedule(1*time.Second, appendStr, "*") - time.Sleep(5 * time.Second) - close(stop) + increase := func() { + count++ + } - fmt.Println(res) //[* * * * *] + stop := function.Schedule(2*time.Second, increase) + + time.Sleep(2 * time.Second) + close(stop) + + fmt.Println(count) + + // Output: + // 2 } ``` - - ### Pipeline

执行函数pipeline.

@@ -313,6 +314,7 @@ func main() { ```go func Pipeline[T any](funcs ...func(T) T) func(T) T ``` + 例子: ```go @@ -325,23 +327,26 @@ import ( func main() { addOne := func(x int) int { - return x + 1 - } - double := func(x int) int { - return 2 * x - } - square := func(x int) int { - return x * x - } + return x + 1 + } + double := func(x int) int { + return 2 * x + } + square := func(x int) int { + return x * x + } - f := Pipeline(addOne, double, square) + fn := function.Pipeline(addOne, double, square) - fmt.Println(fn(2)) //36 + result := fn(2) + + fmt.Println(result) + + // Output: + // 36 } ``` - - ### Watcher

Watcher用于记录代码执行时间。可以启动/停止/重置手表定时器。获取函数执行的时间。

@@ -350,9 +355,9 @@ func main() { ```go type Watcher struct { - startTime int64 - stopTime int64 - excuting bool + startTime int64 + stopTime int64 + excuting bool } func NewWatcher() *Watcher func (w *Watcher) Start() //start the watcher @@ -360,6 +365,7 @@ func (w *Watcher) Stop() //stop the watcher func (w *Watcher) Reset() //reset the watcher func (w *Watcher) GetElapsedTime() time.Duration //get the elapsed time of function execution ``` + 例子: ```go @@ -373,30 +379,27 @@ import ( func main() { w := function.NewWatcher() - w.Start() + w.Start() - longRunningTask() + longRunningTask() - fmt.Println(w.excuting) //true + fmt.Println(w.excuting) //true - w.Stop() + w.Stop() - eapsedTime := w.GetElapsedTime().Milliseconds() - - fmt.Println(eapsedTime) + eapsedTime := w.GetElapsedTime().Milliseconds() - w.Reset() + fmt.Println(eapsedTime) + + w.Reset() } func longRunningTask() { - var slice []int64 - for i := 0; i < 10000000; i++ { - slice = append(slice, int64(i)) - } + var slice []int64 + for i := 0; i < 10000000; i++ { + slice = append(slice, int64(i)) + } } ``` - - - diff --git a/docs/maputil.md b/docs/maputil.md index ed9241e..6da6fd3 100644 --- a/docs/maputil.md +++ b/docs/maputil.md @@ -1,16 +1,17 @@ # Maputil + Package maputil includes some functions to manipulate map.
## Source: -- [https://github.com/duke-git/lancet/blob/main/maputil/map.go](https://github.com/duke-git/lancet/blob/main/maputil/map.go) - +- [https://github.com/duke-git/lancet/blob/main/maputil/map.go](https://github.com/duke-git/lancet/blob/main/maputil/map.go)
## Example: + ```go import ( "github.com/duke-git/lancet/v2/maputil" @@ -20,22 +21,22 @@ import (
## Index -- [ForEach](#ForEach) -- [Filter](#Filter) -- [Intersect](#Intersect) -- [Keys](#Keys) -- [Merge](#Merge) -- [Minus](#Minus) -- [Values](#Values) -- [IsDisjoint](#IsDisjoint) + +- [ForEach](#ForEach) +- [Filter](#Filter) +- [Intersect](#Intersect) +- [Keys](#Keys) +- [Merge](#Merge) +- [Minus](#Minus) +- [Values](#Values) +- [IsDisjoint](#IsDisjoint)
## Documentation - - ### ForEach +

Executes iteratee funcation for every key and value pair in map.

Signature: @@ -43,6 +44,7 @@ import ( ```go func ForEach[K comparable, V any](m map[K]V, iteratee func(key K, value V)) ``` + Example: ```go @@ -54,26 +56,28 @@ import ( ) func main() { - m := map[string]int{ - "a": 1, - "b": 2, - "c": 3, - "d": 4, - } + m := map[string]int{ + "a": 1, + "b": 2, + "c": 3, + "d": 4, + } - var sum int + var sum int - maputil.ForEach(m, func(_ string, value int) { - sum += value - }) - fmt.Println(sum) // 10 + maputil.ForEach(m, func(_ string, value int) { + sum += value + }) + + fmt.Println(sum) + + // Output: + // 10 } ``` - - - ### Filter +

Iterates over map, return a new map contains all key and value pairs pass the predicate function.

Signature: @@ -81,6 +85,7 @@ func main() { ```go func Filter[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) map[K]V ``` + Example: ```go @@ -92,29 +97,32 @@ import ( ) func main() { - m := map[string]int{ - "a": 1, - "b": 2, - "c": 3, - "d": 4, - "e": 5, - } - isEven := func(_ string, value int) bool { - return value%2 == 0 - } + m := map[string]int{ + "a": 1, + "b": 2, + "c": 3, + "d": 4, + "e": 5, + } + isEven := func(_ string, value int) bool { + return value%2 == 0 + } - maputil.Filter(m, func(_ string, value int) { - sum += value - }) - res := maputil.Filter(m, isEven) - fmt.Println(res) // map[string]int{"b": 2, "d": 4,} + maputil.Filter(m, func(_ string, value int) { + sum += value + }) + + result := maputil.Filter(m, isEven) + + fmt.Println(result) + + // Output: + // map[b:2 d:4] } ``` - - - ### Intersect +

Iterates over maps, return a new map of key and value pairs in all given maps.

Signature: @@ -122,6 +130,7 @@ func main() { ```go func Intersect[K comparable, V any](maps ...map[K]V) map[K]V ``` + Example: ```go @@ -133,37 +142,42 @@ import ( ) func main() { - m1 := map[string]int{ - "a": 1, - "b": 2, - "c": 3, - } + m1 := map[string]int{ + "a": 1, + "b": 2, + "c": 3, + } - m2 := map[string]int{ - "a": 1, - "b": 2, - "c": 6, - "d": 7, - } + m2 := map[string]int{ + "a": 1, + "b": 2, + "c": 6, + "d": 7, + } - m3 := map[string]int{ - "a": 1, - "b": 9, - "e": 9, - } + m3 := map[string]int{ + "a": 1, + "b": 9, + "e": 9, + } - fmt.Println(maputil.Intersect(m1)) // map[string]int{"a": 1, "b": 2, "c": 3} + result1 := maputil.Intersect(m1) + result2 := maputil.Intersect(m1, m2) + result3 := maputil.Intersect(m1, m2, m3) - fmt.Println(maputil.Intersect(m1, m2)) // map[string]int{"a": 1, "b": 2} + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) - fmt.Println(maputil.Intersect(m1, m2, m3)) // map[string]int{"a": 1} + // Output: + // map[a:1 b:2 c:3] + // map[a:1 b:2] + // map[a:1] } ``` - - - ### Keys +

Returns a slice of the map's keys.

Signature: @@ -171,6 +185,7 @@ func main() { ```go func Keys[K comparable, V any](m map[K]V) []K ``` + Example: ```go @@ -182,24 +197,26 @@ import ( ) func main() { - m := map[int]string{ - 1: "a", - 2: "a", - 3: "b", - 4: "c", - 5: "d", - } + m := map[int]string{ + 1: "a", + 2: "a", + 3: "b", + 4: "c", + 5: "d", + } - keys := maputil.Keys(m) - sort.Ints(keys) - fmt.Println(keys) // []int{1, 2, 3, 4, 5} + keys := maputil.Keys(m) + sort.Ints(keys) + + fmt.Println(keys) + + // Output: + // [1 2 3 4 5] } ``` - - - ### Merge +

Merge maps, next key will overwrite previous key.

Signature: @@ -207,6 +224,7 @@ func main() { ```go func Merge[K comparable, V any](maps ...map[K]V) map[K]V ``` + Example: ```go @@ -218,22 +236,26 @@ import ( ) func main() { - m1 := map[int]string{ - 1: "a", - 2: "b", - } - m2 := map[int]string{ - 1: "1", - 3: "2", - } - fmt.Println(maputil.Merge(m1, m2)) // map[int]string{1:"1", 2:"b", 3:"2",} + m1 := map[int]string{ + 1: "a", + 2: "b", + } + m2 := map[int]string{ + 1: "1", + 3: "2", + } + + result := maputil.Merge(m1, m2) + + fmt.Println(result) + + // Output: + // map[1:c 2:b 3:d] } ``` - - - ### Minus +

Creates an map of whose key in mapA but not in mapB.

Signature: @@ -241,6 +263,7 @@ func main() { ```go func Minus[K comparable, V any](mapA, mapB map[K]V) map[K]V ``` + Example: ```go @@ -252,25 +275,29 @@ import ( ) func main() { - m1 := map[string]int{ - "a": 1, - "b": 2, - "c": 3, - } + m1 := map[string]int{ + "a": 1, + "b": 2, + "c": 3, + } - m2 := map[string]int{ - "a": 11, - "b": 22, - "d": 33, - } + m2 := map[string]int{ + "a": 11, + "b": 22, + "d": 33, + } + + result := maputil.Minus(m1, m2) - fmt.Println(maputil.Minus(m1, m2)) //map[string]int{"c": 3} + fmt.Println(result) + + // Output: + // map[c:3] } ``` - - ### Values +

Returns a slice of the map's values.

Signature: @@ -278,6 +305,7 @@ func main() { ```go func Values[K comparable, V any](m map[K]V) []V ``` + Example: ```go @@ -289,22 +317,26 @@ import ( ) func main() { - m := map[int]string{ - 1: "a", - 2: "a", - 3: "b", - 4: "c", - 5: "d", - } + m := map[int]string{ + 1: "a", + 2: "a", + 3: "b", + 4: "c", + 5: "d", + } - values := maputil.Values(m) - sort.Strings(values) + values := maputil.Values(m) + sort.Strings(values) - fmt.Println(values) // []string{"a", "a", "b", "c", "d"} + fmt.Println(values) + + // Output: + // [a a b c d] } ``` ### IsDisjoint +

Checks two maps are disjoint if they have no keys in common

Signature: @@ -312,6 +344,7 @@ func main() { ```go func IsDisjoint[K comparable, V any](mapA, mapB map[K]V) bool ``` + Example: ```go @@ -323,30 +356,28 @@ import ( ) func main() { - m1 := map[int]string{ - 1: "a", - 2: "a", - 3: "b", - 4: "c", - 5: "d", - } + m1 := map[string]int{ + "a": 1, + "b": 2, + "c": 3, + } - m2 := map[int]string{ - 1: "a", - 2: "a", - 3: "b", - 4: "c", - 5: "d", - } + m2 := map[string]int{ + "d": 22, + } - m3 := map[int]string{ - 6: "a", - } + m3 := map[string]int{ + "a": 22, + } - ok := maputil.IsDisjoint(m2, m1) - fmt.Println(ok) // false + result1 := maputil.IsDisjoint(m1, m2) + result2 := maputil.IsDisjoint(m1, m3) - ok = maputil.IsDisjoint(m2, m3) - fmt.Println(ok) // true + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false } -``` \ No newline at end of file +``` diff --git a/docs/maputil_zh-CN.md b/docs/maputil_zh-CN.md index fd25dd1..d06aa69 100644 --- a/docs/maputil_zh-CN.md +++ b/docs/maputil_zh-CN.md @@ -1,16 +1,17 @@ # Maputil -maputil包包括一些操作map的函数。 + +maputil 包包括一些操作 map 的函数。
## 源码: -- [https://github.com/duke-git/lancet/blob/main/maputil/map.go](https://github.com/duke-git/lancet/blob/main/maputil/map.go) - +- [https://github.com/duke-git/lancet/blob/main/maputil/map.go](https://github.com/duke-git/lancet/blob/main/maputil/map.go)
## 用法: + ```go import ( "github.com/duke-git/lancet/v2/maputil" @@ -20,22 +21,22 @@ import (
## 目录: -- [ForEach](#ForEach) -- [Filter](#Filter) -- [Intersect](#Intersect) -- [Keys](#Keys) -- [Merge](#Merge) -- [Minus](#Minus) -- [Values](#Values) -- [IsDisjoint](#IsDisjoint) + +- [ForEach](#ForEach) +- [Filter](#Filter) +- [Intersect](#Intersect) +- [Keys](#Keys) +- [Merge](#Merge) +- [Minus](#Minus) +- [Values](#Values) +- [IsDisjoint](#IsDisjoint)
-## API文档: - - +## API 文档: ### ForEach +

对map中的每对key和value执行iteratee函数

函数签名: @@ -43,6 +44,7 @@ import ( ```go func ForEach[K comparable, V any](m map[K]V, iteratee func(key K, value V)) ``` + 例子: ```go @@ -54,26 +56,28 @@ import ( ) func main() { - m := map[string]int{ - "a": 1, - "b": 2, - "c": 3, - "d": 4, - } + m := map[string]int{ + "a": 1, + "b": 2, + "c": 3, + "d": 4, + } - var sum int + var sum int - maputil.ForEach(m, func(_ string, value int) { - sum += value - }) - fmt.Println(sum) // 10 + maputil.ForEach(m, func(_ string, value int) { + sum += value + }) + + fmt.Println(sum) + + // Output: + // 10 } ``` - - - ### Filter +

迭代map中的每对key和value, 返回符合predicate函数的key, value

函数签名: @@ -81,6 +85,7 @@ func main() { ```go func Filter[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) map[K]V ``` + 例子: ```go @@ -92,29 +97,32 @@ import ( ) func main() { - m := map[string]int{ - "a": 1, - "b": 2, - "c": 3, - "d": 4, - "e": 5, - } - isEven := func(_ string, value int) bool { - return value%2 == 0 - } + m := map[string]int{ + "a": 1, + "b": 2, + "c": 3, + "d": 4, + "e": 5, + } + isEven := func(_ string, value int) bool { + return value%2 == 0 + } - maputil.Filter(m, func(_ string, value int) { - sum += value - }) - res := maputil.Filter(m, isEven) - fmt.Println(res) // map[string]int{"b": 2, "d": 4,} + maputil.Filter(m, func(_ string, value int) { + sum += value + }) + + result := Filter(m, isEven) + + fmt.Println(result) + + // Output: + // map[b:2 d:4] } ``` - - - ### Intersect +

多个map的交集操作

函数签名: @@ -122,6 +130,7 @@ func main() { ```go func Intersect[K comparable, V any](maps ...map[K]V) map[K]V ``` + 例子: ```go @@ -133,37 +142,42 @@ import ( ) func main() { - m1 := map[string]int{ - "a": 1, - "b": 2, - "c": 3, - } + m1 := map[string]int{ + "a": 1, + "b": 2, + "c": 3, + } - m2 := map[string]int{ - "a": 1, - "b": 2, - "c": 6, - "d": 7, - } + m2 := map[string]int{ + "a": 1, + "b": 2, + "c": 6, + "d": 7, + } - m3 := map[string]int{ - "a": 1, - "b": 9, - "e": 9, - } + m3 := map[string]int{ + "a": 1, + "b": 9, + "e": 9, + } - fmt.Println(maputil.Intersect(m1)) // map[string]int{"a": 1, "b": 2, "c": 3} + result1 := maputil.Intersect(m1) + result2 := maputil.Intersect(m1, m2) + result3 := maputil.Intersect(m1, m2, m3) - fmt.Println(maputil.Intersect(m1, m2)) // map[string]int{"a": 1, "b": 2} + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) - fmt.Println(maputil.Intersect(m1, m2, m3)) // map[string]int{"a": 1} + // Output: + // map[a:1 b:2 c:3] + // map[a:1 b:2] + // map[a:1] } ``` - - - ### Keys +

返回map中所有key的切片

函数签名: @@ -171,6 +185,7 @@ func main() { ```go func Keys[K comparable, V any](m map[K]V) []K ``` + 例子: ```go @@ -182,24 +197,26 @@ import ( ) func main() { - m := map[int]string{ - 1: "a", - 2: "a", - 3: "b", - 4: "c", - 5: "d", - } + m := map[int]string{ + 1: "a", + 2: "a", + 3: "b", + 4: "c", + 5: "d", + } - keys := maputil.Keys(m) - sort.Ints(keys) - fmt.Println(keys) // []int{1, 2, 3, 4, 5} + keys := maputil.Keys(m) + sort.Ints(keys) + + fmt.Println(keys) + + // Output: + // [1 2 3 4 5] } ``` - - - ### Merge +

合并多个maps, 相同的key会被后来的key覆盖

函数签名: @@ -207,6 +224,7 @@ func main() { ```go func Merge[K comparable, V any](maps ...map[K]V) map[K]V ``` + 例子: ```go @@ -218,21 +236,26 @@ import ( ) func main() { - m1 := map[int]string{ - 1: "a", - 2: "b", - } - m2 := map[int]string{ - 1: "1", - 3: "2", - } - fmt.Println(maputil.Merge(m1, m2)) // map[int]string{1:"1", 2:"b", 3:"2",} + m1 := map[int]string{ + 1: "a", + 2: "b", + } + m2 := map[int]string{ + 1: "1", + 3: "2", + } + + result := maputil.Merge(m1, m2) + + fmt.Println(result) + + // Output: + // map[1:c 2:b 3:d] } ``` - - ### Minus +

返回一个map,其中的key存在于mapA,不存在于mapB.

函数签名: @@ -240,6 +263,7 @@ func main() { ```go func Minus[K comparable, V any](mapA, mapB map[K]V) map[K]V ``` + 例子: ```go @@ -251,25 +275,29 @@ import ( ) func main() { - m1 := map[string]int{ - "a": 1, - "b": 2, - "c": 3, - } + m1 := map[string]int{ + "a": 1, + "b": 2, + "c": 3, + } - m2 := map[string]int{ - "a": 11, - "b": 22, - "d": 33, - } + m2 := map[string]int{ + "a": 11, + "b": 22, + "d": 33, + } - fmt.Println(maputil.Minus(m1, m2)) //map[string]int{"c": 3} + result := maputil.Minus(m1, m2) + + fmt.Println(result) + + // Output: + // map[c:3] } ``` - - ### Values +

返回map中所有value的切片

函数签名: @@ -277,6 +305,7 @@ func main() { ```go func Values[K comparable, V any](m map[K]V) []V ``` + 例子: ```go @@ -288,23 +317,24 @@ import ( ) func main() { - m := map[int]string{ - 1: "a", - 2: "a", - 3: "b", - 4: "c", - 5: "d", - } + m := map[int]string{ + 1: "a", + 2: "a", + 3: "b", + 4: "c", + 5: "d", + } - values := maputil.Values(m) - sort.Strings(values) + values := maputil.Values(m) + sort.Strings(values) - fmt.Println(values) // []string{"a", "a", "b", "c", "d"} + // Output: + // [a a b c d] } ``` - ### IsDisjoint +

验证两个map是否具有不同的key

函数签名: @@ -312,6 +342,7 @@ func main() { ```go func IsDisjoint[K comparable, V any](mapA, mapB map[K]V) bool ``` + 例子: ```go @@ -323,30 +354,28 @@ import ( ) func main() { - m1 := map[int]string{ - 1: "a", - 2: "a", - 3: "b", - 4: "c", - 5: "d", - } + m1 := map[string]int{ + "a": 1, + "b": 2, + "c": 3, + } - m2 := map[int]string{ - 1: "a", - 2: "a", - 3: "b", - 4: "c", - 5: "d", - } + m2 := map[string]int{ + "d": 22, + } - m3 := map[int]string{ - 6: "a", - } + m3 := map[string]int{ + "a": 22, + } - ok := maputil.IsDisjoint(m2, m1) - fmt.Println(ok) // false + result1 := maputil.IsDisjoint(m1, m2) + result2 := maputil.IsDisjoint(m1, m3) - ok = maputil.IsDisjoint(m2, m3) - fmt.Println(ok) // true + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false } -``` \ No newline at end of file +``` diff --git a/docs/mathutil.md b/docs/mathutil.md index 4ff3b3f..24190f8 100644 --- a/docs/mathutil.md +++ b/docs/mathutil.md @@ -1,16 +1,17 @@ # Mathutil + Package mathutil implements some functions for math calculation.
## Source: -- [https://github.com/duke-git/lancet/blob/main/mathutil/mathutil.go](https://github.com/duke-git/lancet/blob/main/mathutil/mathutil.go) - +- [https://github.com/duke-git/lancet/blob/main/mathutil/mathutil.go](https://github.com/duke-git/lancet/blob/main/mathutil/mathutil.go)
## Example: + ```go import ( "github.com/duke-git/lancet/v2/mathutil" @@ -20,26 +21,26 @@ import (
## Index -- [Average](#Average) -- [Exponent](#Exponent) -- [Fibonacci](#Fibonacci) -- [Factorial](#Factorial) -- [Max](#Max) -- [MaxBy](#MaxBy) -- [Min](#Min) -- [MinBy](#MaxBy) -- [Percent](#Percent) -- [RoundToFloat](#RoundToFloat) -- [RoundToString](#RoundToString) -- [TruncRound](#TruncRound) + +- [Average](#Average) +- [Exponent](#Exponent) +- [Fibonacci](#Fibonacci) +- [Factorial](#Factorial) +- [Max](#Max) +- [MaxBy](#MaxBy) +- [Min](#Min) +- [MinBy](#MaxBy) +- [Percent](#Percent) +- [RoundToFloat](#RoundToFloat) +- [RoundToString](#RoundToString) +- [TruncRound](#TruncRound)
## Documentation - - ### Average +

Return average value of numbers. Maybe call RoundToFloat to round result.

Signature: @@ -47,6 +48,7 @@ import ( ```go func Average[T constraints.Integer | constraints.Float](numbers ...T) T ``` + Example: ```go @@ -58,16 +60,22 @@ import ( ) func main() { - fmt.Println(mathutil.Average(0, 0)) //0 - fmt.Println(mathutil.Average(1, 1)) //1 - avg := mathutil.Average(1.2, 1.4) //1.2999999998 - roundAvg := mmathutil.RoundToFloat(avg, 1) // 1.3 + result1 := mathutil.Average(1, 2) + + avg := mathutil.Average(1.2, 1.4) + result2 := mathutil.RoundToFloat(avg, 1) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // 1 + // 1.3 } ``` - - ### Exponent +

Calculate x to the nth power.

Signature: @@ -75,6 +83,7 @@ func main() { ```go func Exponent(x, n int64) int64 ``` + Example: ```go @@ -86,15 +95,23 @@ import ( ) func main() { - fmt.Println(mathutil.Exponent(10, 0)) //1 - fmt.Println(mathutil.Exponent(10, 1)) //10 - fmt.Println(mathutil.Exponent(10, 2)) //100 + result1 := mathutil.Exponent(10, 0) + result2 := mathutil.Exponent(10, 1) + result3 := mathutil.Exponent(10, 2) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // 1 + // 10 + // 100 } ``` - - ### Fibonacci +

Calculate the nth number of fibonacci sequence.

Signature: @@ -102,6 +119,7 @@ func main() { ```go func Fibonacci(first, second, n int) int ``` + Example: ```go @@ -113,17 +131,23 @@ import ( ) func main() { - fmt.Println(mathutil.Fibonacci(1, 1, 1)) //1 - fmt.Println(mathutil.Fibonacci(1, 1, 2)) //1 - fmt.Println(mathutil.Fibonacci(1, 1, 3)) //2 - fmt.Println(mathutil.Fibonacci(1, 1, 4)) //3 - fmt.Println(mathutil.Fibonacci(1, 1, 5)) //5 + result1 := mathutil.Fibonacci(1, 1, 1) + result2 := mathutil.Fibonacci(1, 1, 2) + result3 := mathutil.Fibonacci(1, 1, 5) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // 1 + // 1 + // 5 } ``` - - ### Factorial +

Calculate the factorial of x.

Signature: @@ -131,6 +155,7 @@ func main() { ```go func Factorial(x uint) uint ``` + Example: ```go @@ -142,16 +167,23 @@ import ( ) func main() { - fmt.Println(mathutil.Factorial(0)) //1 - fmt.Println(mathutil.Factorial(1)) //1 - fmt.Println(mathutil.Factorial(2)) //2 - fmt.Println(mathutil.Factorial(3)) //6 + result1 := mathutil.Factorial(1) + result2 := mathutil.Factorial(2) + result3 := mathutil.Factorial(3) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // 1 + // 2 + // 6 } ``` - - ### Max +

Return max value of numbers.

Signature: @@ -159,6 +191,7 @@ func main() { ```go func Max[T constraints.Integer | constraints.Float](numbers ...T) T ``` + Example: ```go @@ -170,23 +203,28 @@ import ( ) func main() { - fmt.Println(mathutil.Max(0, 0)) //0 - fmt.Println(mathutil.Max(1, 2, 3)) //3 - fmt.Println(mathutil.Max(1.2, 1.4, 1.1, 1.4)) //1.4 + result1 := mathutil.Max(1, 2, 3) + result2 := mathutil.Max(1.2, 1.4, 1.1, 1.4) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // 3 + // 1.4 } ``` - - - ### MaxBy +

Return the maximum value of a slice using the given comparator function.

Signature: ```go -func MaxBy[T any](slice []T, comparator func(T, T) bool) T +func MaxBy[T any](slice []T, comparator func(T, T) bool) T ``` + Example: ```go @@ -198,26 +236,31 @@ import ( ) func main() { - res1 := mathutil.MaxBy([]string{"a", "ab", "abc"}, func(v1, v2 string) bool { - return len(v1) > len(v2) - }) - fmt.Println(res1) //abc + result1 := mathutil.MaxBy([]string{"a", "ab", "abc"}, func(v1, v2 string) bool { + return len(v1) > len(v2) + }) - res2 := mathutil.MaxBy([]string{"abd", "abc", "ab"}, func(v1, v2 string) bool { - return len(v1) > len(v2) - }) - fmt.Println(res2) //abd + result2 := mathutil.MaxBy([]string{"abd", "abc", "ab"}, func(v1, v2 string) bool { + return len(v1) > len(v2) + }) - res3 := mathutil.MaxBy([]string{}, func(v1, v2 string) bool { - return len(v1) > len(v2) - }) - fmt.Println(res3) //“” + result3 := mathutil.MaxBy([]string{}, func(v1, v2 string) bool { + return len(v1) > len(v2) + }) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // abc + // abd + // } ``` - - ### Min +

Return the minimum value of numbers.

Signature: @@ -225,6 +268,7 @@ func main() { ```go func Min[T constraints.Integer | constraints.Float](numbers ...T) T ``` + Example: ```go @@ -236,22 +280,28 @@ import ( ) func main() { - fmt.Println(mathutil.Min(0, 0)) //0 - fmt.Println(mathutil.Min(1, 2, 3)) //1 - fmt.Println(mathutil.Min(1.2, 1.4, 1.1, 1.4)) //1.1 + result1 := mathutil.Min(1, 2, 3) + result2 := mathutil.Min(1.2, 1.4, 1.1, 1.4) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // 1 + // 1.1 } ``` - - ### MinBy +

Return the minimum value of a slice using the given comparator function.

Signature: ```go -func MinBy[T any](slice []T, comparator func(T, T) bool) T +func MinBy[T any](slice []T, comparator func(T, T) bool) T ``` + Example: ```go @@ -263,27 +313,31 @@ import ( ) func main() { - res1 := mathutil.MinBy([]string{"a", "ab", "abc"}, func(v1, v2 string) bool { - return len(v1) < len(v2) - }) - fmt.Println(res1) //a + result1 := mathutil.MinBy([]string{"a", "ab", "abc"}, func(v1, v2 string) bool { + return len(v1) < len(v2) + }) - res2 := mathutil.MinBy([]string{"ab", "ac", "abc"}, func(v1, v2 string) bool { - return len(v1) < len(v2) - }) - fmt.Println(res2) //ab + result2 := mathutil.MinBy([]string{"ab", "ac", "abc"}, func(v1, v2 string) bool { + return len(v1) < len(v2) + }) - res3 := mathutil.MinBy([]string{}, func(v1, v2 string) bool { - return len(v1) < len(v2) - }) - fmt.Println(res3) //“” + result3 := mathutil.MinBy([]string{}, func(v1, v2 string) bool { + return len(v1) < len(v2) + }) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // a + // ab + // } ``` - - - ### Percent +

calculate the percentage of val to total, retain n decimal places.

Signature: @@ -291,6 +345,7 @@ func main() { ```go func Percent(val, total float64, n int) float64 ``` + Example: ```go @@ -302,14 +357,20 @@ import ( ) func main() { - fmt.Println(mathutil.Percent(1, 2, 2)) //0.5 - fmt.Println(mathutil.Percent(0.1, 0.3, 2)) //0.33 + result1 := mathutil.Percent(1, 2, 2) + result2 := mathutil.Percent(0.1, 0.3, 2) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // 0.5 + // 0.33 } ``` - - ### RoundToFloat +

Round float up to n decimal places.

Signature: @@ -317,6 +378,7 @@ func main() { ```go func RoundToFloat(x float64, n int) float64 ``` + Example: ```go @@ -328,18 +390,23 @@ import ( ) func main() { - fmt.Println(mathutil.RoundToFloat(0, 0)) //0 - fmt.Println(mathutil.RoundToFloat(0, 1)) //0 - fmt.Println(mathutil.RoundToFloat(0.124, 2)) //0.12 - fmt.Println(mathutil.RoundToFloat(0.125, 2)) //0.13 - fmt.Println(mathutil.RoundToFloat(0.125, 3)) //0.125 + result1 := mathutil.RoundToFloat(0.124, 2) + result2 := mathutil.RoundToFloat(0.125, 2) + result3 := mathutil.RoundToFloat(0.125, 3) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // 0.12 + // 0.13 + // 0.125 } ``` - - - ### RoundToString +

Round float up to n decimal places. will return string.

Signature: @@ -347,6 +414,7 @@ func main() { ```go func RoundToString(x float64, n int) string ``` + Example: ```go @@ -358,17 +426,23 @@ import ( ) func main() { - fmt.Println(mathutil.RoundToString(0, 0)) //"0" - fmt.Println(mathutil.RoundToString(0, 1)) //"0.0: - fmt.Println(mathutil.RoundToString(0.124, 2)) //"0.12" - fmt.Println(mathutil.RoundToString(0.125, 2)) //"0.13" - fmt.Println(mathutil.RoundToString(0.125, 3)) //"0.125" + result1 := mathutil.RoundToString(0.124, 2) + result2 := mathutil.RoundToString(0.125, 2) + result3 := mathutil.RoundToString(0.125, 3) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // 0.12 + // 0.13 + // 0.125 } ``` - - ### TruncRound +

Round float off n decimal places.

Signature: @@ -376,6 +450,7 @@ func main() { ```go func TruncRound(x float64, n int) float64 ``` + Example: ```go @@ -387,13 +462,17 @@ import ( ) func main() { - fmt.Println(mathutil.TruncRound(0, 0)) //0 - fmt.Println(mathutil.TruncRound(0, 1)) //0 - fmt.Println(mathutil.TruncRound(0.124, 2)) //0.12 - fmt.Println(mathutil.TruncRound(0.125, 2)) //0.12 - fmt.Println(mathutil.TruncRound(0.125, 3)) //0.125 + result1 := mathutil.TruncRound(0.124, 2) + result2 := mathutil.TruncRound(0.125, 2) + result3 := mathutil.TruncRound(0.125, 3) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // 0.12 + // 0.12 + // 0.125 } ``` - - - diff --git a/docs/mathutil_zh-CN.md b/docs/mathutil_zh-CN.md index fc0f905..c6fc845 100644 --- a/docs/mathutil_zh-CN.md +++ b/docs/mathutil_zh-CN.md @@ -1,16 +1,17 @@ # Mathutil -mathutil包实现了一些数学计算的函数. + +mathutil 包实现了一些数学计算的函数.
## 源码: -- [https://github.com/duke-git/lancet/blob/main/mathutil/mathutil.go](https://github.com/duke-git/lancet/blob/main/mathutil/mathutil.go) - +- [https://github.com/duke-git/lancet/blob/main/mathutil/mathutil.go](https://github.com/duke-git/lancet/blob/main/mathutil/mathutil.go)
## 用法: + ```go import ( "github.com/duke-git/lancet/v2/mathutil" @@ -20,25 +21,26 @@ import (
## 目录 -- [Average](#Average) -- [Exponent](#Exponent) -- [Fibonacci](#Fibonacci) -- [Factorial](#Factorial) -- [Max](#Max) -- [MaxBy](#MaxBy) -- [Min](#Min) -- [MinBy](#MaxBy) -- [Percent](#Percent) -- [RoundToFloat](#RoundToFloat) -- [RoundToString](#RoundToString) -- [TruncRound](#TruncRound) + +- [Average](#Average) +- [Exponent](#Exponent) +- [Fibonacci](#Fibonacci) +- [Factorial](#Factorial) +- [Max](#Max) +- [MaxBy](#MaxBy) +- [Min](#Min) +- [MinBy](#MaxBy) +- [Percent](#Percent) +- [RoundToFloat](#RoundToFloat) +- [RoundToString](#RoundToString) +- [TruncRound](#TruncRound)
## Documentation - ### Average +

计算平均数. 可能需要对结果调用RoundToFloat方法四舍五入

函数签名: @@ -46,6 +48,7 @@ import ( ```go func Average[T constraints.Integer | constraints.Float](numbers ...T) T ``` + 例子: ```go @@ -57,15 +60,22 @@ import ( ) func main() { - fmt.Println(mathutil.Average(0, 0)) //0 - fmt.Println(mathutil.Average(1, 1)) //1 - avg := mathutil.Average(1.2, 1.4) //1.2999999998 - roundAvg := mmathutil.RoundToFloat(avg, 1) // 1.3 + result1 := mathutil.Average(1, 2) + + avg := mathutil.Average(1.2, 1.4) + result2 := mathutil.RoundToFloat(avg, 1) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // 1 + // 1.3 } ``` - ### Exponent +

指数计算(x的n次方)

函数签名: @@ -73,6 +83,7 @@ func main() { ```go func Exponent(x, n int64) int64 ``` + 例子: ```go @@ -84,15 +95,23 @@ import ( ) func main() { - fmt.Println(mathutil.Exponent(10, 0)) //1 - fmt.Println(mathutil.Exponent(10, 1)) //10 - fmt.Println(mathutil.Exponent(10, 2)) //100 + result1 := mathutil.Exponent(10, 0) + result2 := mathutil.Exponent(10, 1) + result3 := mathutil.Exponent(10, 2) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // 1 + // 10 + // 100 } ``` - - ### Fibonacci +

计算斐波那契数列的第n个数

函数签名: @@ -100,6 +119,7 @@ func main() { ```go func Fibonacci(first, second, n int) int ``` + 例子: ```go @@ -111,17 +131,23 @@ import ( ) func main() { - fmt.Println(mathutil.Fibonacci(1, 1, 1)) //1 - fmt.Println(mathutil.Fibonacci(1, 1, 2)) //1 - fmt.Println(mathutil.Fibonacci(1, 1, 3)) //2 - fmt.Println(mathutil.Fibonacci(1, 1, 4)) //3 - fmt.Println(mathutil.Fibonacci(1, 1, 5)) //5 + result1 := mathutil.Fibonacci(1, 1, 1) + result2 := mathutil.Fibonacci(1, 1, 2) + result3 := mathutil.Fibonacci(1, 1, 5) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // 1 + // 1 + // 5 } ``` - - ### Factorial +

计算阶乘

函数签名: @@ -129,6 +155,7 @@ func main() { ```go func Factorial(x uint) uint ``` + 例子: ```go @@ -140,15 +167,23 @@ import ( ) func main() { - fmt.Println(mathutil.Factorial(0)) //1 - fmt.Println(mathutil.Factorial(1)) //1 - fmt.Println(mathutil.Factorial(2)) //2 - fmt.Println(mathutil.Factorial(3)) //6 + result1 := mathutil.Factorial(1) + result2 := mathutil.Factorial(2) + result3 := mathutil.Factorial(3) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // 1 + // 2 + // 6 } ``` - ### Max +

返回参数中的最大数

函数签名: @@ -156,6 +191,7 @@ func main() { ```go func Max[T constraints.Integer | constraints.Float](numbers ...T) T ``` + 例子: ```go @@ -167,22 +203,28 @@ import ( ) func main() { - fmt.Println(mathutil.Max(0, 0)) //0 - fmt.Println(mathutil.Max(1, 2, 3)) //3 - fmt.Println(mathutil.Max(1.2, 1.4, 1.1, 1.4)) //1.4 + result1 := mathutil.Max(1, 2, 3) + result2 := mathutil.Max(1.2, 1.4, 1.1, 1.4) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // 3 + // 1.4 } ``` - - ### MaxBy +

使用给定的比较器函数返回切片的最大值

函数签名: ```go -func MaxBy[T any](slice []T, comparator func(T, T) bool) T +func MaxBy[T any](slice []T, comparator func(T, T) bool) T ``` + 例子: ```go @@ -194,27 +236,31 @@ import ( ) func main() { - res1 := mathutil.MaxBy([]string{"a", "ab", "abc"}, func(v1, v2 string) bool { - return len(v1) > len(v2) - }) - fmt.Println(res1) //abc + result1 := mathutil.MaxBy([]string{"a", "ab", "abc"}, func(v1, v2 string) bool { + return len(v1) > len(v2) + }) - res2 := mathutil.MaxBy([]string{"abd", "abc", "ab"}, func(v1, v2 string) bool { - return len(v1) > len(v2) - }) - fmt.Println(res2) //abd + result2 := mathutil.MaxBy([]string{"abd", "abc", "ab"}, func(v1, v2 string) bool { + return len(v1) > len(v2) + }) - res3 := mathutil.MaxBy([]string{}, func(v1, v2 string) bool { - return len(v1) > len(v2) - }) - fmt.Println(res3) //“” + result3 := mathutil.MaxBy([]string{}, func(v1, v2 string) bool { + return len(v1) > len(v2) + }) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // abc + // abd + // } ``` - - - ### Min +

返回参数中的最小数

函数签名: @@ -222,6 +268,7 @@ func main() { ```go func Min[T constraints.Integer | constraints.Float](numbers ...T) T ``` + 例子: ```go @@ -233,22 +280,28 @@ import ( ) func main() { - fmt.Println(mathutil.Min(0, 0)) //0 - fmt.Println(mathutil.Min(1, 2, 3)) //1 - fmt.Println(mathutil.Min(1.2, 1.4, 1.1, 1.4)) //1.1 + result1 := mathutil.Min(1, 2, 3) + result2 := mathutil.Min(1.2, 1.4, 1.1, 1.4) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // 1 + // 1.1 } ``` - - ### MinBy +

使用给定的比较器函数返回切片的最小值

函数签名: ```go -func MinBy[T any](slice []T, comparator func(T, T) bool) T +func MinBy[T any](slice []T, comparator func(T, T) bool) T ``` + 例子: ```go @@ -260,27 +313,31 @@ import ( ) func main() { - res1 := mathutil.MinBy([]string{"a", "ab", "abc"}, func(v1, v2 string) bool { - return len(v1) < len(v2) - }) - fmt.Println(res1) //a + result1 := mathutil.MinBy([]string{"a", "ab", "abc"}, func(v1, v2 string) bool { + return len(v1) < len(v2) + }) - res2 := mathutil.MinBy([]string{"ab", "ac", "abc"}, func(v1, v2 string) bool { - return len(v1) < len(v2) - }) - fmt.Println(res2) //ab + result2 := mathutil.MinBy([]string{"ab", "ac", "abc"}, func(v1, v2 string) bool { + return len(v1) < len(v2) + }) - res3 := mathutil.MinBy([]string{}, func(v1, v2 string) bool { - return len(v1) < len(v2) - }) - fmt.Println(res3) //“” + result3 := mathutil.MinBy([]string{}, func(v1, v2 string) bool { + return len(v1) < len(v2) + }) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // a + // ab + // } ``` - - - ### Percent +

计算百分比,保留n位小数

函数签名: @@ -288,6 +345,7 @@ func main() { ```go func Percent(val, total float64, n int) float64 ``` + 例子: ```go @@ -299,14 +357,20 @@ import ( ) func main() { - fmt.Println(mathutil.Percent(1, 2, 2)) //0.5 - fmt.Println(mathutil.Percent(0.1, 0.3, 2)) //0.33 + result1 := mathutil.Percent(1, 2, 2) + result2 := mathutil.Percent(0.1, 0.3, 2) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // 0.5 + // 0.33 } ``` - - ### RoundToFloat +

四舍五入,保留n位小数

函数签名: @@ -314,6 +378,7 @@ func main() { ```go func RoundToFloat(x float64, n int) float64 ``` + 例子: ```go @@ -325,18 +390,23 @@ import ( ) func main() { - fmt.Println(mathutil.RoundToFloat(0, 0)) //0 - fmt.Println(mathutil.RoundToFloat(0, 1)) //0 - fmt.Println(mathutil.RoundToFloat(0.124, 2)) //0.12 - fmt.Println(mathutil.RoundToFloat(0.125, 2)) //0.13 - fmt.Println(mathutil.RoundToFloat(0.125, 3)) //0.125 + result1 := mathutil.RoundToFloat(0.124, 2) + result2 := mathutil.RoundToFloat(0.125, 2) + result3 := mathutil.RoundToFloat(0.125, 3) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // 0.12 + // 0.13 + // 0.125 } ``` - - - ### RoundToString +

四舍五入,保留n位小数,返回字符串

函数签名: @@ -344,6 +414,7 @@ func main() { ```go func RoundToString(x float64, n int) string ``` + 例子: ```go @@ -355,17 +426,23 @@ import ( ) func main() { - fmt.Println(mathutil.RoundToString(0, 0)) //"0" - fmt.Println(mathutil.RoundToString(0, 1)) //"0.0: - fmt.Println(mathutil.RoundToString(0.124, 2)) //"0.12" - fmt.Println(mathutil.RoundToString(0.125, 2)) //"0.13" - fmt.Println(mathutil.RoundToString(0.125, 3)) //"0.125" + result1 := mathutil.RoundToString(0.124, 2) + result2 := mathutil.RoundToString(0.125, 2) + result3 := mathutil.RoundToString(0.125, 3) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // 0.12 + // 0.13 + // 0.125 } ``` - - ### TruncRound +

截短n位小数(不进行四舍五入)

函数签名: @@ -373,6 +450,7 @@ func main() { ```go func TruncRound(x float64, n int) float64 ``` + 例子: ```go @@ -384,13 +462,17 @@ import ( ) func main() { - fmt.Println(mathutil.TruncRound(0, 0)) //0 - fmt.Println(mathutil.TruncRound(0, 1)) //0 - fmt.Println(mathutil.TruncRound(0.124, 2)) //0.12 - fmt.Println(mathutil.TruncRound(0.125, 2)) //0.12 - fmt.Println(mathutil.TruncRound(0.125, 3)) //0.125 + result1 := mathutil.TruncRound(0.124, 2) + result2 := mathutil.TruncRound(0.125, 2) + result3 := mathutil.TruncRound(0.125, 3) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // 0.12 + // 0.12 + // 0.125 } ``` - - -