最近总结了一组常用的util类
package main
import (
"fmt"
mapset "github.com/deckarep/golang-set"
"github.com/pkg/errors"
"strings"
"time"
)
func main() {
testUtil()
}
func testUtil() {
a := []int{1, 2, 1, 3, 4}
b := []int{4, 3, 5, 6}
//多个数组合并去重
c := MergeDuplicateIntArray(a, b)
fmt.Println(c)
//单个数组去重
d := DuplicateIntArray(a)
fmt.Println(d)
//取不同
e := GetDifferentIntArray(a, b)
fmt.Println(e)
//判断数组有某个元素
flag := ExistIntArray(a, 1)
fmt.Println(a, "存在", 1, flag)
str1 := []string{"a", "b", "c", "a", "c"}
str2 := []string{"b", "a", "d", "e", "f"}
//多字符串数组去重合并
str3 := MergeDuplicateStringArray(str1, str2)
fmt.Println(str3)
//单个字符串数组去重
str4 := DuplicateStringArray(str1)
fmt.Println(str4)
//取不同
str5 := GetDifferentStringArray(str1, str2)
fmt.Println(str5)
//字符串数组判断存在某个字符串
flag2 := ExistStringArray(str1, "a")
fmt.Println(str1, "存在", "a", flag2)
//时间转换
timeStr := "2019-01-02 15:04:05"
parseTimeReason := "时间转换原因"
t, err := ParseTimeByTimeStr(timeStr, parseTimeReason)
if err != nil {
fmt.Println(err.Error())
Catch(err)
}
fmt.Println(t)
//获取int64的时间戳
time1 := ParseTimeToInt64()
fmt.Println("当前时间:", time1)
time2 := ParseTimeToInt64(t)
fmt.Println("2019-01-02 15:04:05转换时间:", time2)
now := time.Now()
//获取最近的周一
time3 := ParseCurrentMonday(now)
fmt.Println(time3)
//返回某一天的当地时区0点
time4 := ParseMorningTime(now)
fmt.Println(time4)
//当月第一天0点
time5 := ParseFirstDayOfMonthMorning(now)
fmt.Println(time5)
//获取传入时间前一天的时间,不传默认是昨天
time6 := ParseYesterdayTime(now)
fmt.Println(time6)
//把int64转换成1993-12-26 10:30:00
time7 := ParseTimeToInt64(time6)
timeStr2 := ParseTimeToTimeStr(time7)
fmt.Println(timeStr2)
}
//合并两个数组并去重
func MergeDuplicateIntArray(slice []int, elems []int) []int {
listPId := append(slice, elems...)
t := mapset.NewSet()
for _, i := range listPId {
t.Add(i)
}
var result []int
for i := range t.Iterator().C {
result = append(result, i.(int))
}
return result
}
//数组去重
func DuplicateIntArray(m []int) []int {
s := make([]int, 0)
smap := make(map[int]int)
for _, value := range m {
//计算map长度
length := len(smap)
smap[value] = 1
//比较map长度, 如果map长度不相等, 说明key不存在
if len(smap) != length {
s = append(s, value)
}
}
return s
}
//数组取出不同元素 放入结果
//sourceList中的元素不在sourceList2中 则取到result中
func GetDifferentIntArray(sourceList, sourceList2 []int) (result []int) {
for _, src := range sourceList {
var find bool
for _, target := range sourceList2 {
if src == target {
find = true
continue
}
}
if !find {
result = append(result, src)
}
}
return
}
//数组存在某个数字
func ExistIntArray(s []int, e int) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
//字符串数组存在某个字符串
func ExistStringArray(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
//数组取出不同元素 放入结果
//sourceList中的元素不在sourceList2中 则取到result中
func GetDifferentStringArray(sourceList, sourceList2 []string) (result []string) {
for _, src := range sourceList {
var find bool
for _, target := range sourceList2 {
if src == target {
find = true
continue
}
}
if !find {
result = append(result, src)
}
}
return
}
//合并两个字符串数组并去重
func MergeDuplicateStringArray(slice []string, elems []string) []string {
listPId := append(slice, elems...)
t := mapset.NewSet()
for _, i := range listPId {
t.Add(i)
}
var result []string
for i := range t.Iterator().C {
result = append(result, i.(string))
}
return result
}
//字符串数组去重
func DuplicateStringArray(m []string) []string {
s := make([]string, 0)
smap := make(map[string]string)
for _, value := range m {
//计算map长度
length := len(smap)
smap[value] = value
//比较map长度, 如果map长度不相等, 说明key不存在
if len(smap) != length {
s = append(s, value)
}
}
return s
}
//时间转换 将1993-12-26 10:30:00转换为time
func ParseTimeByTimeStr(str, errPrefix string) (time.Time, error) {
p := strings.TrimSpace(str)
if p == "" {
return time.Time{}, errors.Errorf("%s不能为空", errPrefix)
}
t, err := time.ParseInLocation("2006-01-02 15:04:05", str, time.Local)
if err != nil {
return time.Time{}, errors.Errorf("%s格式错误", errPrefix)
}
return t, nil
}
//获取int64 当前时间戳/输入time时间戳
func ParseTimeToInt64(t ...time.Time) int64 {
if len(t) == 0 {
return time.Now().UnixNano() / 1e6
} else {
return t[0].UnixNano() / 1e6
}
}
//获取int64 秒
func ParseSecondTimeToInt64() int64 {
return time.Now().Unix()
}
//获取int64 小时
func ParseHourTimeToInt64() int64 {
return time.Now().Unix() / 3600 * 3600
}
//捕获异常 error
func Catch(err error) {
if err != nil {
panic(err)
}
}
//获取最近的周一
func ParseCurrentMonday(t time.Time) time.Time {
offset := int(time.Monday - t.Weekday())
if offset > 0 {
offset = -6
}
weekStart := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
return weekStart
}
//返回某一天的当地时区0点
func ParseMorningTime(t time.Time) time.Time {
s := t.Format("19931226")
result, _ := time.ParseInLocation("19931226", s, time.Local)
return result
}
//当月第一天0点
func ParseFirstDayOfMonthMorning(t time.Time) time.Time {
if t.IsZero() {
return t
}
return time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, t.Location())
}
//获取传入时间前一天的时间,不传默认是昨天
func ParseYesterdayTime(t ...time.Time) time.Time {
if len(t) == 0 {
return time.Now().AddDate(0, 0, -1)
} else {
return t[0].AddDate(0, 0, -1)
}
}
//把int64转换成1993-12-26 10:30:00
func ParseTimeToTimeStr(intTime int64, strfmt ...string) string {
t := time.Unix(intTime/1e3, 0)
defaultFmt := "2006-01-02 15:04:05"
if len(strfmt) > 0 {
defaultFmt = strfmt[0]
}
return t.Format(defaultFmt)
}
//int64 to time
func Int64ConvertToTime(intTime int64) time.Time {
return time.Unix(intTime/1e3, 0)
}
考虑不周,麻烦看到的也可以补充