草庐IT

可变数组

全部标签

go - switch 语句中的可变范围问题

我有一个程序可以解析日志文件并返回一个结构片段,其中包含来自文件的填充数据。我还编写了一个函数来将结构项添加到上述列表中。但有一个错误提示“无法使用‘sf’(类型*SegmentationFault)作为SegmentationFault类型”,该错误源于此函数。我该如何解决这个问题?func(sfList*SegmentationFaultList)AddItem(itemSegmentationFault)[]SegmentationFault{sfList.Items=append(sfList.Items,item)returnsfList.Items}funcparseLog

arrays - 从数组结构呈现 json 的问题

这个问题在这里已经有了答案:JSONanddealingwithunexportedfields(2个答案)关闭4年前。我用gorilla/mux和mysql数据库做一个简单的休息服务typeCarrostruct{Anoint`json:"ano"`Corstring`json:"cor"`Nomestring`json:"nome""`}typeRevendastruct{carro[]Carrorodastring}functest(whttp.ResponseWriter,r*http.Request){varlistas[]CarrocarA:=Carro{1975,"Ama

json - 如何将 header 添加到 JSON 以识别数组值的数组名称

我正在尝试使用带有GO的encoding/json向JSON中的每个数组添加header。什么意思?想要有这样的东西:{"Dog":[{"breed":"Chihuahua","color":"brown"},{"breed":"Pug","color":"white"}],"Cat":[{"breed":"British","color":"white"},"breed":"Ragdoll","color":"gray"}]}主要思想是在这种情况下有一个“类别”Dog和Cat。我已经有了这个解决方案,但我正在寻找可以改进它的东西。我的代码是这样的:typeDogstruct{Bree

go - 是否可以将可变参数与标志包使用结合起来?

关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭4年前。Improvethisquestion我希望我的函数以高效的方式接受可选参数。阅读类似的帖子让我想到了可变参数,我试图将它与标志包一起实现(只是寻找用户能够运行他们选择的可用命令行标志的任何替代方案。这是我的标志包用法:funcmain(){vartargetstringvarmethodstringflag.StringVar(&target,"target","http://google.com/robots.txt","Targ

mysql - mysql的语法数组

关闭。这个问题需要debuggingdetails.它目前不接受答案。编辑问题以包含desiredbehavior,aspecificproblemorerror,andtheshortestcodenecessarytoreproducetheproblem.这将有助于其他人回答问题。关闭3年前。Improvethisquestion将数据数组添加到数据库的语法是什么,我发现postgresql是:pg.Array//"ins"istheSQLinsertstatementins:="INSERTINTOposts(title,tags)VALUES($1,$2)"//"tags"i

database - 在结构数组中搜索

我有一个这样定义的结构:typeIssuesstruct{RedmineIssuestringGitlabIssuestring}然后我从数据库中获取列表database.Find(&Issues)然后我有另一个数组redmineIssues[]redmine.Issue有没有什么方法可以在我的数组问题中搜索问题,这些问题也在基于字段RedmineIssue(字符串)的数组redmineIssues中?今天是我在做的事database.Find(&Issues)redmineIssue:=[]string{}for_,issueRedmine:=rangeIssues{redmineI

对于 Golang 中的数组,XML Unmarshalling 属性不同

我有一个Web服务,我希望在其中接收到两个不同的请求;一个称为Request,仅包含一个Request,另一个称为RequestBulk,其中包含一组Request。两者都映射到Golang结构,如下所示:typeRequestBulkstruct{XMLNamexml.Name`xml:"https://item.comRequestBulk"`Messagestring`xml:"Message"`Request[]Request`xml:"Request,omitempty"`}typeRequeststruct{XMLNamexml.Name`xml:"https://item

go - 可变 slice 作为参数错误 :cannot initialize 2 variables with 1 value

尝试使用可变参数组合多个slice,我收到错误:无法用1个值初始化2个变量如何调用这个Combine函数?代码如下:funcCombine(ss...[]string)[]string{mp:=map[string]bool{}for_,s:=rangess{for_,v:=ranges{ifv!=""{if_,ok:=mp[v];!ok{mp[v]=true}}}}combined:=[]string{}forv:=rangemp{combined=append(combined,v)}returncombined}tests:=[]struct{caseNamestrings1[]

go - 执行 UnmarshalExtJSON 时读取数组的无效请求

我正在尝试使用go.mongodb.org/mongo-driver/bson中的UnmarshalExtJSON将扩展的JSON解码为结构它给我一个错误:读取数组的请求无效如何将这些数据解码到我的结构中?MVCE:packagemainimport("fmt""go.mongodb.org/mongo-driver/bson")funcmain(){varjson="{\"data\":{\"streamInformation\":{\"codecs\":[\"avc1.640028\"]}}}"varworkflowWorkflowe:=bson.UnmarshalExtJSON(

go - 在 golang 的函数参数中省略数组类型

这个问题在这里已经有了答案:Typeconvertingslicesofinterfaces(9个回答)关闭3年前。我正在编写一个写入方法,将一个值数组写入InfluxDB我想要的是能够拥有类似的东西:func(influxClient*InfluxClient)Write(myArray[]interface{})(error){fmt.Print(myArray)//InsertintoDBreturnnil}其中myArray可以是一个包含任何对象的数组我尝试使用myArray[]interface{}省略myArray的类型,但它不起作用,我得到:Cannotuse'meter