golang 字符串常用操作
go 数组或者切片转字符串分割
FlashPromotionSessionId c
strings.Replace(strings.Trim(fmt.Sprint(FlashPromotionSessionId), "[]"), " ", ",", -1)
fmt.Sprint(interface{}) string 任意类型转字符串
strings.Trim(string,"[]") 去除字符串两边中包含的中括号
strings.Replace(sting,"被替换",“替换符号”)
s2 = strings.Join(sli, ",")
s2 = strings.Split(sli, ",")
字符串截取
str :="abc" //str[0,1]=a str[1,1]=b
//判断字符串s中是否包含个子串str中的任何一个字符。包含则返回true,
//如果str为空则返回false
index := strings.ContainsAny(s,str)
fmt.Println(index) //true
os.Setenv("MSF_ENV", "dev") 定义自定义环境变量
if os.Getenv("MSF_ENV") == "dev" 获取环境变量
//时间库
var dateUnix int64
dateTime :=time.Unix(dateUnix,0) //int64 转时间戳 time类型
dateTime.Format("2006-01-02") //数据格式化 年月日
timeRes := time.Now().Sub(ReviewTime) //当前时间和ReviewTime 计算时间差
timeRes := time.Now().Add(ReviewTime) //当前时间和ReviewTime 计算时间合
//int 类型
num :=1.3
math.Floor(num+0.5) //实现四舍五入
//float64 浮点保留后两位
num := 3.456
num, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", num), 64)//3.46
Math.floor(15.7784514000 * 100) / 100 // 输出结果为 15.77
go语言 实现 今天、明天 ,昨天、本周、本月的时间
now := time.Now()
week := now.Weekday()
offset := int(time.Monday-week)
if offset > 0 {
offset = -6
}
fmt.Println(offset)
//周
StartDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
EndDates := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 999999999, time.Local)
fmt.Println("周一",StartDate.AddDate(0, 0, offset))//周一
fmt.Println("周末",EndDates.AddDate(0, 0,int((week+1)-week)))//周末
//月
currentYear,currentMonth,_ := now.Date()
currentLocation := now.Location()
firstOfMonth := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, currentLocation)//月初开始
EndOfMonth := time.Date(now.Year(), now.Month(), 1, 23, 59, 59, 999999999, currentLocation)//月初开始
lastOfMonth := EndOfMonth.AddDate(0, 1, -1)//月底结束
fmt.Println("月初",firstOfMonth)
fmt.Println("月末",lastOfMonth)
//今天
firstOfDay := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation)//今天开始
lastOfDay := time.Date(currentYear, currentMonth, 1, 23, 59, 59, 999999999, currentLocation)//今天结束
fmt.Println("今天开始",firstOfDay.AddDate(0,0,int(now.Day()-1)))
fmt.Println("今天结束",lastOfDay.AddDate(0,0,int(now.Day()-1)))
//昨天
firstOfYesterday := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, time.Local)//昨天开始
lastOfYesterday := time.Date(currentYear, currentMonth, 1, 23, 59, 59, 999999999, time.Local)//昨天结束
fmt.Println("昨天开始",firstOfYesterday.AddDate(0,0,int(now.Day()-2)))
fmt.Println("昨天结束",lastOfYesterday.AddDate(0,0,int(now.Day()-2)))
fmt.Println("明天开始",time.Date(currentTime.Year(), currentTime.Month(), currentTime.Day()+1, 0, 0, 0, 0, currentTime.Location()).Unix())
fmt.Println("明天结束",time.Date(currentTime.Year(), currentTime.Month(), currentTime.Day()+args.EffectiveDays+1, 23, 59, 59, 0, currentTime.Location()).Unix())
04-02
1389

03-22
360
