草庐IT

TIME_TICK

全部标签

go - `int * time.Second` 什么时候工作,什么时候不在 golang 中?

为什么time.Sleep(5*time.Second)工作正常,但是:x:=180time.Sleep(15/x*60*time.Second)不是吗?我收到类型不匹配错误(类型int64和time.Duration)。鉴于错误,我更了解后者为何失败,而不是前者为何成功。 最佳答案 在Go中,一个numericliteral(e.g.60)isanuntypedconstant.这意味着它将被静默地强制转换为适合它所使用的操作的任何类型。所以当你说:varx:=5*time.Second然后根据time.Second推断类型为ti

go - 为什么 golang time.Equal 中没有定位人员?

在http://golang.org/src/pkg/time/time.go62//Equalreportswhethertandurepresentthesametimeinstant.63//Twotimescanbeequaleveniftheyareindifferentlocations.64//Forexample,6:00+0200CESTand4:00UTCareEqual.65//Thiscomparisonisdifferentfromusingt==u,whichalsocompares66//thelocations.67func(tTime)Equal(uT

go - time.Since() 的奇怪行为

我正在运行一个GO(1.9.2)程序,我的代码类似于:startTime:=time.Now()......fmt.Printf("%v(1)%v\n",user.uid,int64(time.Since(startTime)))fmt.Printf("%v(F)%v\n",user.uid,int64(time.Since(startTime)))(两个fmt语句在连续的行上)我预计打印输出的时间会差不多,但这里是打印的一些结果:921(1)2000100921(F)3040173800(3秒)360(1)2000100360(F)1063060800(1秒)447(1)400020

go - 为什么 Go 的 time.Parse() 不解析时区标识符?

考虑以下代码:packagemainimport("time""fmt")const(format="2006010215:04MST"date="2018080112:00EDT")funcmain(){aloc,_:=time.LoadLocation("America/New_York")eloc,_:=time.LoadLocation("Europe/Berlin")tn,_:=time.Parse(format,date)tl,_:=time.ParseInLocation(format,date,aloc)fmt.Println(tn)//Says+0000despite

Golang tour Switch 求值顺序 : time. Now().Weekday() + 2 yields runtime error: index out of range

我正在学习Golang,正在浏览我找到关于切换评估顺序的教程的导览。我对它做了一些修改(例如周六到周日),只是为了玩玩。它打印太远了。即使是星期天。因此,我将代码修改为如下所示:packagemainimport("fmt""time")funcmain(){day:=time.Mondayfmt.Printf("When's%v?\n",day)today:=time.Now().Weekday()switchday{casetoday+0:fmt.Println("Today.")casetoday+1:fmt.Println("Tomorrow.",today+1)casetod

mongodb - 在 mongodb 记录中使用 time.Time

我要在集合中插入新项目。为此使用官方mongogo驱动程序(https://github.com/mongodb/mongo-go-driver)。collection.InsertOne(context.Background(),map[string]interface{}{"string":"test","integer":123,"float":0.123,"array":[]string{"t","e","s","t"},"objectid":objectid.New(),"time":time.Now(),})但结果我遇到了几个属性的问题:time.Time和objectid

time - 如何从非英语字符串中解析月份

我想在go中将以下字符串解析为日期:"ThisitemwillbereleasedonMarch9,2014."我关注了this并想出了:funcfindReleaseDateString(rawstring)time.Time{test,err:=time.Parse("ThisitemwillbereleasedonJanuary2,2006.",raw)iferr!=nil{panic(err)}returntest}这就像英文字符串的魅力。我的问题:我想解析德语字符串。喜欢:"DieserArtikelwirdam9.März2014erscheinen."我知道,我可以通过正

go - time.Sleep() 是否屈服于其他 goroutines?

在Go中,对time.Sleep()的调用会产生给其他goroutines吗?我有一种感觉,但在其他答案中(例如:Understandinggoroutines)time.Sleep没有明确列为调度点。 最佳答案 是的。参见Pre-emptioninthescheduler.Inpriorreleases,agoroutinethatwasloopingforevercouldstarveoutothergoroutinesonthesamethread,aseriousproblemwhenGOMAXPROCSprovidedon

go - 如何在 Golang 中使用 'time.After' 和 'default'?

我正在尝试理解Golang例程的简单代码:packagemainimport("fmt""time")funcsleep(secondsint,endSignalchan很好,但是为什么我不能在这个“选择”block中使用简单的默认值?像这样:for!end{select{caseend=它得到这个输出:❯gorungoroutines-timeout.goNoendsignalreceived!Noendsignalreceived!Noendsignalreceived!Noendsignalreceived!...Theend!我不明白为什么。 最佳答

date - Go 语言 time.Parse() 用于没有时区的时间戳

在Go中,我尝试使用time.Parse()time包中的函数将字符串时间戳转换为Time对象。我知道Go有一种不常见的方式来表示时间戳所用的时间格式,方法是向其提供引用时间(MonJan215:04:05-0700MST2006)的示例以您的格式显示。但是,我仍然遇到错误问题。这是我的时间戳之一的示例:TueNov2709:09:29UTC2012这是我正在调用的电话:t,err:=time.Parse("MonJan0222:04:05UTC2006","TueNov2709:09:29UTC2012")基本上我在这里所做的就是尝试匹配日期名称/月份名称/日期编号的格式、小时/分钟