草庐IT

TIME_SLICE

全部标签

arrays - 如何在golang中递增数组或 slice 中的所有元素

下面的代码仅递增该slice的第i个元素。有什么内置的东西可以让所有元素加1。请提出建议。fori:=0;i 最佳答案 在使用slice时,您会发现您会倾向于使用for循环。Go没有您可能会在其他语言中找到的用于slice的额外函数。fori:=rangeslice{slice[i]++} 关于arrays-如何在golang中递增数组或slice中的所有元素,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.

json - 将复杂的 JSON 解码为结构并访问数据( slice 的 slice )

一段时间以来,我一直在为这个问题而烦恼。我有一个JSON文件,它必须采用以下格式,我需要在Go中迭代并使用IF语句:[[{"configName":"customer"},{"config":[{"emailSubject":"Newcustomeradded"},{"text":"Hitest2"},{"text":"added2"}]}][{"configName":"customerAndUser"},{"config":[{"emailSubject":"Newcustomeradded"},{"text":"Hi,test1"},{"text":"added1"}]}]]我想

pointers - go - 修改指向 slice 的指针不会修改原始指针

我尝试创建一个结构,其中一个字段是指向现有空slice的指针。然后我修改结构体的字段,稍后我尝试从原始slice中获取新内容-但它仍然是空的!这是演示代码:packagemainimport("fmt")typeAstruct{B[]int}funcmain(){c:=[]int{}a:=&A{B:c,}a.B=append(a.B,5)fmt.Println(c)fmt.Println(a)}这里的结果是:[]&{[5]}问题是——如何从原始slice和结构的字段中获取实际的slice内容?我在这里没有将slice作为参数传递给函数。 最佳答案

list - 戈朗 : create a slice of strutcs

我想创建一个结构列表,函数可以按照go-github中的方式返回这些结构包。但是创建和填充这样一个列表的正确方法是什么?我找到了两种方法,例如,使用append():...allowedRepos:=strings.Fields("repo1,repo2")actualRepos:=[]Repos{}actualRepos=append(actualRepos,Repos{Name:"repo1",URL:"gth.com/repo1"})actualRepos=append(actualRepos,Repos{Name:"repo2",URL:"gth.com/repo2"})...

go - 就地修改 slice 类型的映射值

mp:=map[string][]int{"1":{1,2,3,4},}for_,s:=rangemp{i:=0for_,v:=ranges{ifv%2==0{s[i]=vi++}}s=s[:i]//Itriedmp[k]=sanditworksfine}want:=map[string][]int{"1":{2,4},}if!reflect.DeepEqual(mp,want){fmt.Printf("notexpected")fmt.Println(mp)}使用上面的代码,我无法从slice中删除奇数。我把这个改成了mp:=map[string][]int{"1":{1,2,3,4

json - 尝试将 unix 格式的字符串转换为 time.Time 时,使用 json.Unmarshal 给出 ParseError

我有一个包含以下内容的JSON文件:{..."body":"{\"timestampFrom\":\"154087600\"}"...}当我尝试执行时:iferr:=json.Unmarshal([]byte(apiGatewayEvent.Body),&config);err!=nil{glog.Errorf("ErroroccurredwhiletryingtounmarshalbodyofAPIGatewayProxyRequest.Errormessage-%v",err)returnnil,err}我收到:Erroroccurredwhiletryingtounmarshal

go - 将字符串日期 (Month, Year) 转换为 time.Time

我有一个字符串是:str:="Jan2020"我需要在go中将其转换为time.time格式。请问我该怎么做? 最佳答案 您需要有一个布局字符串来指定如何解析您的字符串。例如:packagemainimport("time""fmt")funcmain(){time,err:=time.Parse("Jan2006","Feb2020")iferr!=nil{panic(err)}fmt.Println(time)}您可能会找到更多关于标准布局的信息here. 关于go-将字符串日期(M

go time.Parser 错误,日期为 DMYYYY

关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭5年前。Improvethisquestion正在解析一个日期,格式为DMYYYY,它抛出一个错误:monthoutofrange日期是:892009代码是:ift,err:=time.Parse("122006","892009");err!=nil{fmt.Println(err.Error())}else{fmt.Println(t)}

api - GoLang, slice 读出错误

我是GoLang的新手,我需要一些帮助。我正在制作简单的API应用程序。通过API将结构传递给slice如下所示:typeStructstruct{//somerecords}varstructs[]Struct//slicefuncSetStruct(whttp.ResponseWriter,req*http.Request){varstStructdecoder:=json.NewDecoder(req.Body)decoder.Decode(&st)emails=append(structs,st)json.NewEncoder(w).Encode(structs)}而且该功能工

go - 如何解决追加数据时第一个参数必须是slice

我正在根据条件进行查询,但是在附加条件时出现错误,我正在进行的查询是:-query:=bson.M{}query["$or"]=[]bson.M{}ifkeyword!=""{query["$or"]=append(query["$or"],bson.M{"author":bson.RegEx{"(?i).*"+keyword+".*","i"}})query["$or"]=append(query["$or"],bson.M{"title":bson.RegEx{"(?i).*"+keyword+".*","i"}})}iftypes==""{query["$or"]=append(