dongyou8087 2015-08-12 20:43
浏览 37

如何覆盖内置类型

By default time.Duration prints it in the format 2h0m0s etc.

However, I want to print it as 2 Hours or 2 Hours 1 Min

One way, I can do that is to create a new struct TimeDurationStruct and create a String() function. I have done the same in golang playground

Working Code

package main

import (
    "fmt"
    "time"
    "math"
)

type TimeDurationStruct struct {
    t time.Duration
}

func (i *TimeDurationStruct) Set(t time.Duration) {
    i.t = t
}

func (i TimeDurationStruct) String() string {
    duration := i.t

    var dayString, hourMinString string
    var days, hours float64

    totalHours := duration.Hours()
    minutes := math.Mod(duration.Minutes(), 60.0)
    if totalHours < 24.0 {
        hours = totalHours
    } else {
        hours = math.Mod(totalHours, 24.0)
        days = totalHours / 24.0
    }

    if hours == 0 {

    } else if hours < 1 {
        if minutes == 1 {
            hourMinString = "1 Min"
        } else {
            hourMinString = fmt.Sprintf("%g Mins", minutes)
        }
    } else if hours >= 1 && hours < 2 {
        if minutes == 0 {
            hourMinString = "1 Hour"
        } else if minutes == 1 {
            hourMinString = "1 Hour 1 Min"
        } else {
            hourMinString = fmt.Sprintf("1 Hour %g Mins", minutes)
        }
    } else {
        if minutes == 0 {
            hourMinString = fmt.Sprintf("%g Hours", hours)
        } else if minutes == 1 {
            hourMinString = fmt.Sprintf("%g Hours 1 Min", hours)
        } else {
            hourMinString = fmt.Sprintf("%g Hours %g Mins", hours, minutes)
        }
    }

    if days > 0 {
        if days == 1 {
            dayString = "1 Day"
        } else {
            dayString = fmt.Sprintf("%g Days ", days)
        }
    }

    return dayString + hourMinString
}

func main() {
    t := time.Now()
    d := t.Add(2 * time.Hour).Sub(t)
    fmt.Println(d)
    var i TimeDurationStruct
    i.Set(d)
    fmt.Println(i)
}

Questions

  1. Instead of a structure type, is it possible to override time.Duration type. E.g

    type TimeDurationStruct time.Duration
    

    I tried it here, but it does not work...

  2. Is there a better/easier way ?

  • 写回答

1条回答 默认 最新

  • doushou6480 2015-08-13 02:44
    关注

    Following on from your question edit, yes you can achieve this effect without a struct. The problem is that when you define your type with:

    type TimeDurationStruct time.Duration
    

    your new TimeDurationStruct type will not inherit any of the methods from time.Duration like you had when you used the type embedding option. You can still access those methods, but you will need to perform a type cast. So the start of your String method should be modified to read something like:

    func (i TimeDurationStruct) String() string {
        duration := time.Duration(i)
        ...
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大