更新一些常用的golang函数(笔记)

in Go with 0 comment

更新一些常用的golang函数(笔记)

带路径的情况下获取文件名称

filepath.Base(filename)

获取文件后缀名

filepath.Ext(filename)

获取文件mime(contentType)类型

mimeType := mime.TypeByExtension(filepath.Ext(filename))

获取当前时间或者增加当前时间(格式化后)

time.Now().Add(duration).Format("2006-01-02 15:04:05")

时间相关处理

format := "2006-01-02 15:04:05"
now := time.Now()
//now, _ := time.Parse(format, time.Now().Format(format))
a, _ := time.Parse(format, "2015-03-10 11:00:00")
b, _ := time.Parse(format, "2015-03-10 16:00:00")

fmt.Println(now.Format(format), a.Format(format), b.Format(format))
fmt.Println(now.After(a))
fmt.Println(now.Before(a))
fmt.Println(now.After(b))
fmt.Println(now.Before(b))
fmt.Println(a.After(b))
fmt.Println(a.Before(b))
fmt.Println(now.Format(format), a.Format(format), b.Format(format))
fmt.Println(now.Unix(), a.Unix(), b.Unix())

设置时区

func Now() time.Time {
    now := time.Now()
    local, _ := time.LoadLocation("local")
    //local, _ := time.LoadLocation("Asia/Shanghai")
    now = now.In(local)
    return now
}

字符串

查找字符串 索引位置
k := "1.jpg"
strings.IndexByte(k, '.jpg')
Comments are closed.