草庐IT

constant_time_test

全部标签

go - 无法将 time.Now() 转换为字符串

我有这个结构://NearbywhatevertypeNearbystruct{idint`json:"id,omitempty"`meint`json:"me,omitempty"`youint`json:"you,omitempty"`contactTimestring`json:"contactTime,omitempty"`}然后我称之为:strconv.Itoa(time.Now())像这样:s1:=Nearby{id:1,me:1,you:2,contactTime:strconv.Itoa(time.Now())}但是它说:>cannotusetime.Now()(typ

unit-testing - 在单元测试中使用随机数与硬编码值

这个问题在这里已经有了答案:WhatarethedownsidesusingrandomvaluesinUnitTesting?(11个答案)RandomdatainUnitTests?(20个答案)关闭3年前。当我写测试的时候,我喜欢用随机数来计算东西。例如funcinit(){rand.Seed(time.Now().UnixNano())}funcTestXYZ(t*testing.T){amount:=rand.Intn(100)cnt:=1+rand.Intn(10)fori:=0;i当然有缺点expected:=calcExpected(amount,cnt)因为需要根据随

go - 打印 time.Time 时出现意外输出

这个问题在这里已经有了答案:Whatisthe"m"intimestampandhowtogettimestampwithout"m"?(3个答案)关闭3年前。“我正在尝试输出一个channel的值,它从一个结构体接收值,它应该是一个字符串和时间。它输出这两个,但随后它包含了这个奇怪的行”+0300+03m=+0.001997101“时间之后。”尝试了fmt包中的许多其他东西,但仍然没有帮助。也尝试过时间包里的东西packagemainimport("fmt""os""os/signal""sync""syscall""time")varwgsync.WaitGrouptypewidg

testing - 我应该使用哪种方法来测试 golang 中的 func main()?

我在main.go中有一个函数main()可以完成这项工作,所有其他函数都在它下面(我没有在这里包括它们)。因此,当我为main中包含的所有funcs编写测试时,我可以测试它们。但是代码覆盖率很低,因为它表明我没有覆盖main函数中的代码。我知道测试库中有一个TestMain函数可以完成这项工作,但我就是不知道如何让它工作,以便测试涵盖funcmain()。下面是我的main()函数,它没有被测试覆盖...funcmain(){c,err:=getConfig()iferr!=nil{log.Fatal(err)}slideshows,err:=getSlideshows(c)ifer

go - 达到 time.Now() 时进行循环

在Golang中是否可以通过给定的日期变量增加for循环中的日期,直到达到当前日期/时间。Now()//Startdatet,_:=time.Parse(time.RFC3339,"2018-07-19T12:25:10.8584224+02:00")//Currentdatect:=time.Now()ford:=t;d.Day()==ct.Day();d=d.AddDate(0,0,1){//Printalldaysbetweenstartdateandcurrentdatefmt.Println(d)}我希望变量d打印出所有日期(包括时间等),直到它到达当前日期

unit-testing - 如何为一小时后函数返回时间编写单元测试

我有以下函数,它在当前时间添加给定的小时数并在中返回新时间funcgetExpiryTime(hourint)*string{constlayout="2006-01-02T15:04:05Z"expiryTime:=time.Now().Local().Add(time.Hour*time.Duration(hour))returnaws.String(expiryTime.Format(layout))}为此功能编写单元测试的最佳方法是什么? 最佳答案 您可以尝试模拟时间提供程序并在您的模拟中设置time.Now()函数以返回您

memory - Go: time.sleep 和内存使用

当运行下面的代码时,程序从1.5M左右开始,然后逐渐增长到6.4M。我想知道为什么。删除time.sleep可解决此问题。有没有办法在默认情况下使用for-select模式并在默认情况下休眠一段时间而不更改任何内存?在sleep后调用runtime.GC()确实可以解决问题。我们可以在不调用GC的情况下实现同样的事情吗?packagemainimport("time")funcmain(){c:=make(chanstruct{})for{select{case同上:packagemainimport("time")funcmain(){c:=make(chanstruct{})for

unit-testing - 去单元测试无法加载文件

1、gotestconfig_test.go,总是文件,error:openconf/config.yml:Thesystemcannotfindthepathspecified2、源码ok3、gotestfile如何在不同目录下成功 最佳答案 测试期间您的工作目录不同。您可能需要动态计算配置目录路径的解决方法,例如:http://www.kaihag.com/external-assets-working-directories-and-go/ 关于unit-testing-去单元测试

unit-testing - 错误 : suite. go:61: test paniced: reflect: Call with too few input arguments

我正在golang中设置单元测试。但是现在我在运行gotest-v时遇到错误。我想解决这个错误并使测试成功。article├client├api│├main.go│├contoroller││├contoroller.go││└contoroller_test.go│├service││├service.go││└service_test.go│├dao││├dao.go││└dao_test.go│├s3││├s3.go││└s3_test.go│├go.mod│├go.sum│└Dockerfile├nginx└docker-compose.yml现在我正在为service.go设

go - 从 time.Now() 开始接收字符串的简单方法

我正在尝试从time.Now()实例中获取日期作为字符串。now:=time.Now()//.String()wouldgivemetheentiredateasastringwhichIdon'tneedday:=now.Day())//iswhatIwantbutasaString.所以string(day)告诉我“无法将day转换为string”。现在对我来说.Day().String()会很好但是没有这样的方法...我现在可以尝试使用time.Now().String()并进行操作,直到一天结束。但应该有更简单的方法来做到这一点...... 最佳答案