Golang工具集-String工具,时间工具,http工具等

gotool是一个全面的Golang工具集合,包括字符串、数组、日期、转换、加密、压缩、文件操作、验证码生成、日志打印、分页和HTTP工具。提供了如StrUtils、DateUtil、ConvertUtils、BcryptUtils等功能,简化了常见的开发任务。
摘要由CSDN通过智能技术生成

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"

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
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
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
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

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
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
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

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
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
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
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
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
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 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

druidcaesa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值