7

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

 3 years ago
source link: https://surest.cn/archives/180/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

更新一些常用的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')

本文由 邓尘锋 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: May 16, 2021 at 03:17 pm


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK