当前位置:   article > 正文

Golang工具集-String工具,时间工具,http工具等_yjjw.xn--dyr923b.net

yjjw.xn--dyr923b.net

gotool

gotool是一个小而全的Golang工具集,主要是将日常开发中常用的到方法进行提炼集成,避免重复造轮子,提高工作效率,每一个方法都是作者经过工作经验,和从以往的项目中提炼出来的。

2021-7-9更新内容详细使用请看文档

  • 添加文件IO操作工具FileUtils
  • 添加验证码生成工具CaptchaUtils
  • 添加文件目录压缩和解压缩工具ZipUtis
  • 字符串数组工具StrArrayUtils

如何使用gotool呢?

安装

go get github.com/druidcaesa/gotool

go.mod github.com/druidcaesa/gotool

引入

import "github.com/druidcaesa/gotool"
  • 1

StrUtils

golang一个string常用工具集,基本涵盖了开发中经常用到的工具,目前正在不端的完善中

1、gotool.StrUtils.ReplacePlaceholder 占位符替换
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
2、gotool.StrUtils.RemoveSuffix 去除文件扩展名获取文件名
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
3、gotool.StrUtils.GetSuffix 获取文件扩展名
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
4、gotool.StrUtils.HasEmpty 判断字符串是否未空,我空返回ture
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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

StrArrayUtils string数组操作工具

1、gotool.StrArrayUtils.StringToInt64 字符串数组转int64数组,调用前请确保字符串数组均为数字
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
2、gotool.StrArrayUtils.StringToInt32 字符串数组转int64数组,调用前请确保字符串数组均为数字
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
3、gotool.StrArrayUtils.ArrayDuplication 数组去重
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

DateUtil

golang一个时间操作工具集,基本涵盖了开发中经常用到的工具,目前正在不端的完善中

1、gotool.DateUtil.FormatToString 时间格式化成字符串
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
2、gotool.DateUtil.IsZero 判断时间是否为空
//时间为空 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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
3、gotool.DateUtil.Now 获取当前时间 等同于time.Now(),为了统一化所以将此方法也纳入到工具中
4、gotool.DateUtil.InterpretStringToTimestamp 字符串格式化成时间类型
//参数一 需要格式化的时间字符串 参数二 字符串格式,需要和需格式化字符串格式一致 
//如 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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
5、gotool.DateUtil.UnixToTime 时间戳转时间
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
6、gotool.DateUtil.GetWeekDay 获取星期几
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
7、gotool.DateUtil.MinuteAddOrSub,HourAddOrSub,DayAddOrSub 时间计算工具
//时间计算
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 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/155830
推荐阅读
相关标签
  

闽ICP备14008679号