Date 如果golang的情况出现问题
我正在计算golang中的日期差,我已经编写了代码,它执行得很好,但当我以分钟为单位计算时间差时,将其转换为整数,并在if条件下进行比较 我正在尝试的代码 根据以下条件,不应打印“diffinmin大于 P“但这是印刷品。我哪里做错了Date 如果golang的情况出现问题,date,datetime,go,Date,Datetime,Go,我正在计算golang中的日期差,我已经编写了代码,它执行得很好,但当我以分钟为单位计算时间差时,将其转换为整数,并在if条件下进行比较 我正在尝试的代码 根据以下条件,不应打印“diffinmin大于 P“但这是印刷品。我哪里做错了 package main import ( "fmt" "time" "strconv" ) func main() { //fetching current time currentTime := time.Now()
package main
import (
"fmt"
"time"
"strconv"
)
func main() {
//fetching current time
currentTime := time.Now()
loc := currentTime.Location()
//past time comes in as string
pasttimestr := "2018-10-18 01:00"
layout := "2006-01-02 15:04"
//converting string to date
pasttime, err := time.ParseInLocation(layout, pasttimestr, loc)
if err != nil {
fmt.Println(err)
}
fmt.Println("Past Time: ", pasttime)
fmt.Println("Current Time: ", currentTime)
//differnce between pastdate and current date
diff := currentTime.Sub(pasttime)
var diffinmin int = int(diff.Minutes())
fmt.Printf( "heloo",diffinmin )
fmt.Printf("time difference is %v or %v in minutes\n", diff, diff.Minutes())
var strconfigtime = "9000"
var mininttimeconfig int
if mininttimeconfig, err := strconv.Atoi(strconfigtime); err == nil {
fmt.Printf("i=%d, type: %T\n", mininttimeconfig, mininttimeconfig)
}
if diffinmin > mininttimeconfig {
fmt.Printf("diffinmin is greater" )
}
}
fiddle:这是关于范围的,您在检查中使用的mininttimeconfig的值为0
if mininttimeconfig, err := strconv.Atoi(strconfigtime); err == nil {
// mininttimeconfig is a new variable scoped to this block
}
// here mininttimeconfig is 0 from declaration line
// var mininttimeconfig int
尝试将行更改为:
var mininttimeconfig int
mininttimeconfig, err = strconv.Atoi(strconfigtime);
if err == nil {
// ...
或:
注意:通常使用错误,如if err!=nil{…}这是关于范围的,您在检查中使用的mininttimeconfig的值为0
if mininttimeconfig, err := strconv.Atoi(strconfigtime); err == nil {
// mininttimeconfig is a new variable scoped to this block
}
// here mininttimeconfig is 0 from declaration line
// var mininttimeconfig int
尝试将行更改为:
var mininttimeconfig int
mininttimeconfig, err = strconv.Atoi(strconfigtime);
if err == nil {
// ...
或:
注意:通常使用错误,如if err!=零{…}