赞
踩
gotool是一个小而全的Golang工具集,主要是将日常开发中常用的到方法进行提炼集成,避免重复造轮子,提高工作效率,每一个方法都是作者经过工作经验,和从以往的项目中提炼出来的。
go get github.com/druidcaesa/gotool
go.mod github.com/druidcaesa/gotool
import "github.com/druidcaesa/gotool"
golang一个string常用工具集,基本涵盖了开发中经常用到的工具,目前正在不端的完善中
func TestStringReplacePlaceholder(t *testing.T) {
s := "你是我的{},我是你的{}"
placeholder, err := gotool.StrUtils.ReplacePlaceholder(s, "唯一", "所有")
if err == nil {
fmt.Println(placeholder)
}
}
//out
== = RUN TestStringReplacePlaceholder
你是我的唯一, 我是你的所有
--- PASS: TestStringReplacePlaceholder (0.00s)
PASS
func TestRemoveSuffix(t *testing.T) {
fullFilename := "test.txt"
suffix, _ := gotool.StrUtils.RemoveSuffix(fullFilename)
fmt.Println(suffix)
fullFilename = "/root/home/test.txt"
suffix, _ = gotool.StrUtils.RemoveSuffix(fullFilename)
fmt.Println(suffix)
}
//out
== = RUN TestRemoveSuffix
test
test
--- PASS: TestRemoveSuffix (0.00s)
PASS
func TestGetSuffix(t *testing.T) {
fullFilename := "test.txt"
suffix, _ := gotool.StrUtils.GetSuffix(fullFilename)
fmt.Println(suffix)
fullFilename = "/root/home/test.txt"
suffix, _ = gotool.StrUtils.GetSuffix(fullFilename)
fmt.Println(suffix)
}
//out
== = RUN TestGetSuffix
.txt
.txt
--- PASS: TestGetSuffix (0.00s)
PASS
func TestHasStr(t *testing.T) { str := "" empty := gotool.StrUtils.HasEmpty(str) fmt.Println(empty) str = "11111" empty = gotool.StrUtils.HasEmpty(str) fmt.Println(empty) } //out == = RUN TestHasStr true false --- PASS: TestHasStr (0.00s) PASS
func TestStringToInt64(t *testing.T) { //字符串数组转int64 strings := []string{ "1", "23123", "232323"} fmt.Println(reflect.TypeOf(strings[0])) toInt64, err := gotool.StrArrayUtils.StringToInt64(strings) if err != nil { t.Fatal(err) } fmt.Println(reflect.TypeOf(toInt64[0])) } //out == = RUN TestStringToInt64 string int64 --- PASS: TestStringToInt64 (0.00s) PASS
func TestStringToInt32(t *testing.T) { //字符串数组转int64 strings := []string{ "1", "23123", "232323"} fmt.Println(reflect.TypeOf(strings[0])) toInt64, err := gotool.StrArrayUtils.StringToInt32(strings) if err != nil { t.Fatal(err) } fmt.Println(reflect.TypeOf(toInt64[0])) } //out == = RUN TestStringToInt32 string int32 --- PASS: TestStringToInt32 (0.00s) PASS
func TestArrayDuplication(t *testing.T) {
//string数组去重
strings := []string{
"hello", "word", "gotool", "word"}
fmt.Println("去重前----------------->", strings)
duplication := gotool.StrArrayUtils.ArrayDuplication(strings)
fmt.Println("去重后----------------->", duplication)
}
//out
== = RUN TestArrayDuplication
去重前-----------------> [hello word gotool word]
去重后-----------------> [hello word gotool]
--- PASS: TestArrayDuplication (0.00s)
PASS
golang一个时间操作工具集,基本涵盖了开发中经常用到的工具,目前正在不端的完善中
func TestFormatToString(t *testing.T) {
now := gotool.DateUtil.Now()
toString := gotool.DateUtil.FormatToString(&now, "YYYY-MM-DD hh:mm:ss")
fmt.Println(toString)
toString = gotool.DateUtil.FormatToString(&now, "YYYYMMDD hhmmss")
fmt.Println(toString)
}
//年月日对应YYYY MM DD 时分秒 hhmmss 可进行任意组合 比如 YYYY hh YYYY-DD hh:mm 等
//out
== = RUN TestFormatToString
2021-07-07 16:13:30
20210707 161330
--- PASS: TestFormatToString (0.00s)
PASS
//时间为空 true 否则 false func TestDate_IsZero(t *testing.T) { t2 := time.Time{ } zero := gotool.DateUtil.IsZero(t2) fmt.Println(zero) zero = gotool.DateUtil.IsZero(gotool.DateUtil.Now()) fmt.Println(zero) } //out == = RUN TestDate_IsZero true false --- PASS: TestDate_IsZero (0.00s) PASS
//参数一 需要格式化的时间字符串 参数二 字符串格式,需要和需格式化字符串格式一致 //如 2021-6-4 对应YYYY-MM-DD 2021.6.4 对应YYYY.MM.DD func TestInterpretStringToTimestamp(t *testing.T) { timestamp, err := gotool.DateUtil.InterpretStringToTimestamp("2021-05-04 15:12:59", "YYYY-MM-DD hh:mm:ss") if err != nil { gotool.Logs.ErrorLog().Println(err.Error()) } fmt.Println(timestamp) } //out == = RUN TestInterpretStringToTimestamp 1620112379 --- PASS: TestInterpretStringToTimestamp (0.00s) PASS
func TestUnixToTime(t *testing.T) {
unix := gotool.DateUtil.Now().Unix()
fmt.Println("时间戳----------------------->", unix)
toTime := gotool.DateUtil.UnixToTime(unix)
fmt.Println(toTime)
}
//out
== = RUN TestUnixToTime
时间戳-----------------------> 1625645682
2021-07-07 16:14:42 +0800 CST
--- PASS: TestUnixToTime (0.00s)
PASS
func TestGetWeekDay(t *testing.T) {
now := gotool.DateUtil.Now()
day := gotool.DateUtil.GetWeekDay(now)
fmt.Println("今天是-----------------周", day)
}
//out
== = RUN TestGetWeekDay
今天是-----------------周 3
--- PASS: TestGetWeekDay (0.00s)
PASS
//时间计算
func TestTimeAddOrSub(t *testing.T) {
now := gotool.DateUtil.Now()
fmt.Println("现在时间是--------------------->", now)
sub := gotool.DateUtil.MinuteAddOrSub(now, 10)
fmt.Println("分钟计算结果-------------------->", sub)
sub = gotool.DateUtil.MinuteAddOrSub(now, -10)
fmt.Println("分钟计算结果-------------------->", sub)
sub = gotool.DateUtil.HourAddOrSub(now, 10)
fmt.Println("小时计算结果-------------------->", sub)
sub
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。