草庐IT

date-and-time-functions

全部标签

go - 如何确保所有 goroutines 在没有 time.Sleep 的情况下终止?

这个问题在这里已经有了答案:Preventthemain()functionfromterminatingbeforegoroutinesfinishinGolang(4个答案)Nooutputfromgoroutine(3个答案)Goroutinedoesnotexecuteiftime.Sleepincluded(1个回答)关闭3年前。我正在尝试使用关于谁先收到消息的go例程。然而,当主goroutine终止时,一些goroutines仍然存在。我通过panic的堆栈跟踪看到了这一点。但是,如果我添加time.Sleep它们都会终止。我猜这是因为,当主要的go例程结束时,Go运行时

function - 云函数部署问题

当我部署云函数时,出现以下错误。我正在使用gomod,我能够从我的沙箱构建和运行所有集成测试,其中一个云函数依赖使用私有(private)githubrepo,当我部署云功能时Go:github.com/myrepo/ptrie@v0.1.:gitfetch-foriginrefs/heads/:refs/heads/refs/tags/:refs/tags/在/builder/pkg/mod/cache/vcs/41e03711c0ecff6d0de8588fa6de21a2c351c59fd4b0a1b685eaaa5868c5892e:退出状态128:致命:无法读取“https:

time - 在 Go 中将 UTC 时间转换为 "local"时间

如何将UTC时间转换为本地时间?我已经为我需要本地时间的所有国家/地区创建了一个具有UTC差异的map。然后我将该差异作为持续时间添加到当前时间(UTC)并打印结果,希望这是该特定国家/地区的本地时间。由于某些原因,结果是错误的。例如Hungary有一个小时的差异。知道为什么我会得到不正确的结果吗?packagemainimport"fmt"import"time"funcmain(){m:=make(map[string]string)m["Hungary"]="+01.00h"offSet,err:=time.ParseDuration(m["Hungary"])iferr!=ni

go - 为什么当函数返回运动员时 Go 是 "athlete declared and not used"?

我正在使用声明的变量返回一个结构。为什么编译器说我没有使用声明的变量?我放置了log.Printf语句来帮助调试错误。为什么log.Printf不算使用变量?import("github.com/gorilla/sessions")funcprofileFromSession(r*http.Request)*workout.Athlete{session,err:=workout.SessionStore.Get(r,defaultSessionID)log.Printf("$$$$$$$$$$$sessioncontains%#v",session)iferr!=nil{log.Pr

amazon-web-services - DynamoDB Marshal 和 unmarshal golang time.Time 自纪元以来以毫秒为单位

SDK默认将time.Time值编码为RFC3339字符串。您如何选择以其他方式编码和解码,例如自纪元以来毫秒?SDK提到了Marshaler和Unmarshaler接口(interface),但没有解释如何使用它们。 最佳答案 (当我正要发布我的问题时,我通过研究UnixTime的工作原理找到了答案)。要使用自定义Marshaler和Unmarshaler,您可以创建自定义类型。typeMillisTimetime.Timefunc(eMillisTime)MarshalDynamoDBAttributeValue(av*dyna

go - 模板 :1: function "copyrightYear" not defined

以下代码在tmp.Execute处出现panic,提示function"copyrightYear"notdefinedimport("os""html/template""fmt")funcmain(){fm:=template.FuncMap{"copyrightYear":func()string{returnfmt.Sprintf("%d",time.Now().Year())},}tmp:=template.Must(template.New("").Parse("{{copyrightYear}}")).Funcs(fm)tmp.Execute(os.Stdout,nil)

高语 : Allocating Slice of Slices in functions results in index out of range

我一直在用Go尝试一些东西,但遇到了一个我无法解决的问题。packagemainimport"fmt"import"strconv"funcwriteHello(iint,){fmt.Printf("hello,world"+strconv.Itoa(i)+"\n")}typeSliceStructstruct{data[][]int;}func(sSliceStruct)New(){s.data=make([][]int,10);}func(sSliceStruct)AllocateSlice(iint){s.data[i]=make([]int,10);}func(sSliceSt

time - 在 Go 中获取 Unix 时间戳的正确方法是什么

在Go中获取Unix时间戳的正确方法是什么?我以为它会是time.Now().Unix()但显然不是。http://play.golang.org/p/KoJADUDxOStime.Now().Unix()告诉我unix时间戳是1257894000而我的浏览器告诉我它是1398351437。www.unixtimestamp.de还告诉我它是1398351704,这是我所期望的。为什么来自Go的那个这么远?顺便说一句,我在本地机器上得到了相同的结果。我错过了什么?更新好吧,事实证明这真的只是play.golang.org上的一个问题。我以为我也在本地测试过它,但我现在又看了一眼,是的,

function - 在 Go 模板中,我可以让 Parse 工作但不能让 ParseFiles 以类似的方式工作。为什么?

我有以下代码:t,err:=template.New("template").Funcs(funcMap).Parse("Howdy{{myfunc.}}")在这种形式下一切正常。但是,如果我对ParseFiles做完全相同的事情,将上面的文本放在template.html中,这是不行的:t,err:=template.New("template").Funcs(funcMap).ParseFiles("template.html")我能够让ParseFiles以下列形式工作,但无法让Funcs生效:t,err:=template.ParseFiles("template.html")

go function input, func (req *AppendEntriesRequest) 编码(w io.Writer) (int, error) {

func(req*AppendEntriesRequest)Encode(wio.Writer)(int,error){pb:=&protobuf.AppendEntriesRequest{Term:proto.Uint64(req.Term),PrevLogIndex:proto.Uint64(req.PrevLogIndex),PrevLogTerm:proto.Uint64(req.PrevLogTerm),CommitIndex:proto.Uint64(req.CommitIndex),LeaderName:proto.String(req.LeaderName),Entri