Go 常用函数

数组

  • 翻转(Reverse):

    for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
         s[i], s[j] = s[j], s[i]
    }
    
  • 排序(Sort):

    • 正序:sort.IntSlice(arr)

    • 倒序:sort.Slice(arr, func(i, j int) bool { return arr[i] > arr[j]})

      ​ 或 sort.Sort(sort.Reverse(sort.IntSlice(a)))

strings包

  • bool: Contains(s, t)、HasPreffix/HasSuffix
  • strings.Replace(s, old, new, n)、ReplaceAll
  • strings.Count (s, t)
  • strings.repeat (s, i) 重复 i 次 s 字符
s := "I_Love_golangCaff"
strings.SplitN() => 指定切割次数    // I  Love_golangCaff
strings.SplitAfter() => 保留分隔符  // I_  Love_   golangCaff
strings.SplitAfterN() => 指定切割次数// I_  Love_golangCaff


- strings.Index(s, t)、LastIndex  // 不存在返回-1
- strings.ToLower(s)ToUpper()ToTitle()
- strings.Trim(s, t)、TrimLeft、TrimRight、TrimSpace、TrimPrefix/TrimSuffix

sort包

  • sort.IntSlice五个方法: Len()、Less()、Swap()、Search()、Sort()
// 按升序排列
func Ints(a []int)                       
func Strings(a []string)
func Float64s(a []float64)  

func IntsAreSorted(a []int) bool       // func SearchInts(a []int, x int) int 二分查找
func StringsAreSorted(a []string) bool   // func SearchStrings(a []string, x string) int
func Float64sAreSorted(a []float64) bool // func SearchFloat64s(a []float64, x float64) int

func Sort(data Interface)
func Stable(data Interface)
func Reverse(data Interface) Interface
func IsSorted(data Interface) bool

// 通用结构
func Slice(slice interface{}, less func(i, j int) bool) 
func SliceStable(slice interface{}, less func(i, j int) bool)
func SliceIsSorted(slice interface{}, less func(i, j int) bool)

time包

now := time.Now() //获取当前时间
now.Year() // Month/Day/Hour/Minute/Second

timestamp := now.Unix() //将时间对象转变为时间戳
now = time.Unix(timestamp, 0) //将时间戳转为时间格式
时间格式化
now.Format("2006-01-02 15:04:05.000 Mon Jan")
now.Format("2006-01-02 03:04:05.000 PM Mon Jan")

now.Format("2006/01/02 15:04")
now.Format("15:04 2006/01/02")
now.Format("2006/01/02")
时间解析
// 加载时区
loc, err := time.LoadLocation("Asia/Shanghai")  
loc := time.Now().Local().Location()


timeObj, err := time.ParseInLocation("2006/01/02 15:04:05", timeStr, loc)
时间间隔
  • func (t Time) Add(d Duration) Time
  • func (t Time) Sub(u Time) Duration
  • Equal() 、 Before()、After()
// time.Duration表示1纳秒,time.Second表示1秒
now := time.Now()
later := now.Add(time.Minute)
interval := later.Sub(now)

常用函数

s := strconv.Itoa(i)       // int_to_string
i, _ := strconv.Atoi(s)    // string_to_int

func Struct2Map(obj interface{}) map[string]string {
   t := reflect.TypeOf(obj)
   v := reflect.ValueOf(obj)
 
   var data = map[string]string{}
   for i := 0; i < t.NumField(); i++ {
      data[strings.ToLower(t.Field(i).Tag.Get("json"))] = v.Field(i).String()
   }
   return data
}
 
func Md5Code(sign string) string {
   h := md5.New()
   h.Write([]byte(sign))
   return hex.EncodeToString(h.Sum(nil))
}
定时器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值