Go-一些简单的类型转换及处理

来源:互联网 发布:mac改变用户名 编辑:程序博客网 时间:2024/05/17 08:32
package mygoimport ("bytes""math""reflect""strconv""time")/*[]string for string*/func ByteString(p []string) string {var resu stringfor _, q := range p {resu = q}return resu}//结构体转为mapfunc StructToMap(obj interface{}) map[string]interface{} {t := reflect.TypeOf(obj)v := reflect.ValueOf(obj)var data = make(map[string]interface{})for i := 0; i < t.NumField(); i++ {data[t.Field(i).Name] = v.Field(i).Interface()}return data}//float小数位func GetAdsStatisCount(num1 int64, num2 int64) float64 {var resu float64resu = float64(num1) / float64(num2)resu = math.Trunc(resu*1e2+0.5) * 1e-2return resu}func MyGoDateFormate(startTs int64, endTs int64) []string {timeLayout := "2006-01-02"           //转化所需模板loc, _ := time.LoadLocation("Local") //重要:获取时区var buf bytes.BufferstartTsTime := time.Unix(startTs/1e3, 0).Format(timeLayout)endTsTime := time.Unix(endTs/1e3, 0).Format(timeLayout)var adsDateList []stringstartTime, _ := time.ParseInLocation(timeLayout, startTsTime, loc) //使用模板在对应时区转化为time.time类型endTime, _ := time.ParseInLocation(timeLayout, endTsTime, loc)subDate := time.Time.Sub(endTime, startTime).Hours()subDay := int(subDate / 24)for i := 0; i <= subDay+1; i++ {hour := strconv.Itoa(24 * i)buf.WriteString(hour)buf.WriteString("h")hh, _ := time.ParseDuration(buf.String())buf.Reset()startSum := startTime.Add(hh)startSr := startSum.Unix()                              //转化为时间戳 类型是int64startTimeSr := time.Unix(startSr, 0).Format(timeLayout) //设置时间戳 使用模板格式化为日期字符串adsDateList = append(adsDateList, startTimeSr)}return adsDateList}//时间戳转换为日期输出func MyGoDateFormateUnix(timestamp int64) string {timeLayout := "2006-01-02" //转化所需模板tampTimeSr := time.Unix(timestamp/1e3, 0).Format(timeLayout)return tampTimeSr}


原创粉丝点击