Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Datetime `foo.Seconds()`(键入time.Duration)错误-无法使用';d、 步骤.秒()';(键入float64)作为类型time.Duration_Datetime_Go - Fatal编程技术网

Datetime `foo.Seconds()`(键入time.Duration)错误-无法使用';d、 步骤.秒()';(键入float64)作为类型time.Duration

Datetime `foo.Seconds()`(键入time.Duration)错误-无法使用';d、 步骤.秒()';(键入float64)作为类型time.Duration,datetime,go,Datetime,Go,我还是个新手…所以 我的问题的症结可以总结为:我正在准备发送到远程API的数据,该API要求此字段为时间.Duration类型,我试图将其作为秒的字符串类型发送##s,并将时间.ParseDuration()转换为秒,但这是一个错误 type Duration struct { Duration time.Duration Step time.Duration } // Needs to return a structure containing a duration

我还是个新手…所以

我的问题的症结可以总结为:我正在准备发送到远程API的数据,该API要求此字段为
时间.Duration
类型,我试图将其作为秒的字符串类型发送
##s
,并将
时间.ParseDuration()
转换为秒,但这是一个错误

type Duration struct {
    Duration time.Duration
    Step     time.Duration
}

// Needs to return a structure containing a duration formatted
// item and an item which is strictly in `seconds` format IE 
// `int` or `float` `+` `"s"`.
func getDuration(pr *PromRequest) (Duration, error) {
    duration, err := time.ParseDuration(pr.TimeRange)
    if err != nil {
        return Duration{}, nil
    }

    timeValue, err := strconv.Atoi(pr.TimeRange[:len(pr.TimeRange)-1])
    if err != nil {
        return Duration{}, err
    }

    // convert day value to day in hours
    step := time.Duration(int64(24*timeValue) * 60 / 14)

    return Duration{
        Duration: duration,
        Step:     step, // <-- should be a time.Duration type...
    }, nil
}

//    \/\/hatever
func getMetricsWithRange(pr *PromRequest) (model.Value, error) {
    d, err := getDuration(pr)
    if err != nil {
        e := fmt.Errorf("unable to translate received time into proper parsed time signature: %s", err)
        fmt.Println(e)
        return nil, err
    }

...
    r := v1.Range{
        Start: time.Now().Add(-d.Duration),
        End:   time.Now(),
        Step:  d.Step.Seconds(),  // <-- error lands here stating it's a float64
    }
...
}

type Duration结构{
持续时间,持续时间
步进时间。持续时间
}
//需要返回包含已格式化的持续时间的结构
//项目和严格采用“秒”格式的项目,即
//`int`或`float`+`s`。
func getDuration(pr*PromRequest)(持续时间,错误){
持续时间,错误:=time.ParseDuration(pr.TimeRange)
如果错误!=零{
返回持续时间{},零
}
timeValue,err:=strconv.Atoi(pr.TimeRange[:len(pr.TimeRange)-1])
如果错误!=零{
返回持续时间{},错误
}
//以小时为单位将日值转换为日
步长:=时间持续时间(int64(24*timeValue)*60/14)
返回持续时间{
持续时间:持续时间,
步骤:步骤,//
Duration.Seconds()
返回一个
float64
值,因为持续时间值可能不是整秒,它可能有十进制数字来表示精确的持续时间。如果
Step
变量是一个四舍五入到最接近的秒的持续时间,请使用
Duration.Round(time.second)

这将给你一个四舍五入到整秒的持续时间

如果要获取秒值,这不是持续时间,而是一个整数:

...
Step: int(Step.Seconds())

但是,在这种情况下,产生的
步骤
不是一个持续时间,而只是一个给出秒数的int值。

谢谢你的回答。我仍在努力解决这个问题。我认为我的问题的关键可以归结为以下几点。我只是准备将数据发送到远程API,该API要求此字段为
time.Du定量
仅以秒为单位键入。显然,这是不可能做到的?@Jim,time.Duration是以纳秒为单位的持续时间,您不能重新定义它。您必须在time.Duration和以秒为单位的某个值之间做出决定,正如答案中所述,这只是一个浮点或整数。@Jim,如果远程API使用
time.Duration
,则它无法获得se仅conds值,但您可以将持续时间四舍五入到精确的秒数。如果要发送秒数值,则它不能是
时间。持续时间
1043s
是(或至少看起来像)字符串:它有一个字母
s
。您可以将其存储在
字符串
[]字节
但不是任何数字数据类型。你可以取一个纳秒值除以10亿得到秒,这就是
func(d Duration)seconds()
时间
包中的float64
。但是,如果您将其存储为浮点或整数,则肯定无法获得以字母
s
结尾的内容,您必须将其格式化为
字符串
[]字节
并添加
s
。我对你的目标一点也不清楚。你想要
时间、持续时间
字符串
、还是
整数
?你已经直接或间接地声明了所有这三个。我试图澄清我试图实现的目标。希望现在它更有意义。
...
Step: int(Step.Seconds())