草庐IT

文章结构

全部标签

JSON 解码器忽略结构字段标签?

我正在使用encoding/json包中的Decoder将JSON配置文件解码为结构。字段名称在文件和结构中具有不同的大小写(由于可见性问题,结构中的第一个字符为小写),因此我使用结构字段标签,如documentation中所述。.问题是解码器似乎忽略了这些标签并且结构字段为空。知道我的代码有什么问题吗?配置.json{"DataSourceName":"simple-blog.db"}配置结构typeConfigstruct{dataSourceNamestring`json:"DataSourceName"`}加载配置funcloadConfig(fileNamestring){f

go - 如何将接口(interface) {} 转换为结构

我一直在寻找如何将接口(interface)转换为结构,但我不知道如何做不到。我会尽力解释我的问题。typeResultstruct{Http_codeintHttp_msgstringResponseinterface{}}此结构由向服务器发出HTTP请求的函数返回,另一方面,我有不同类型的结构来包装响应。这是我要转换接口(interface)的结构。typeResHealthstruct{TypestringGet_healthstruct{Healthybool}}我的问题是,当我尝试做出断言时,我总是遇到段冲突或程序无法编译。工作流程是:packagetesttypeResul

go - 在不同类型的结构之间复制公共(public)字段

我有两个结构体,它们的类型如下:typeUserStructstruct{UserIDstring`bson:"user_id"json:"user_id"`Addressstring`bson:"address"json:"address"`Emailstring`bson:"email"json:"email"`CreatedAttime.Time`bson:"created_at"json:"created_at"`PhoneNumberstring`bson:"phone_number"json:"phone_number"`PanCardstring`bson:"pancar

go - 如何构建复杂的go数据结构

我有以下数据结构来构建和发送数据。然后转成json写成文件。我需要一个数组。它的元素是一张map。map["targets"]值是一个数组,map["labels"]是另一个map。如何构建复杂的数据结构?[{"targets":["10.11.150.1:7870","10.11.150.4:7870"],"labels":{"job":"mysql"}},{"targets":["10.11.122.11:6001","10.11.122.15:6002"],"labels":{"job":"postgres"}}]~ 最佳答案

go - 从 Go 中的另一个嵌套结构变量访问结构的变量

给出了我编写的2个结构。typeDNSPacketstruct{headerDNSHeader.DNSHeaderquestions[]DNSQuestion.DNSQuestionanswers[]DNSRecord.DNSRecordauthorities[]DNSRecord.DNSRecordresources[]DNSRecord.DNSRecord}typeDNSHeaderstruct{iduint16//16bitsrecursion_desiredbool//1bittruncated_messagebool//1bitauthoritative_answerbool

Go结构标签抛出错误: "field tag must be a string"

我是第一次使用GO,正在设置一个小示例API。在尝试从我创建的结构返回JSON对象时,当我将结构标记添加到我的字段时出现此错误:“字段标签必须是字符串”和“无效字符字面量(超过一个字符)”。这是我的代码分解。我在这里缺少什么?packagemainimport("encoding/json""fmt""log""net/http""github.com/gorilla/mux")funcmain(){router:=mux.NewRouter()router.HandleFunc("/demo/v1/version",getVersion).Methods("GET")log.Fata

oop - 将继承的结构作为基础对象传回

我确定这是一个语法问题,我还没有用Go弄清楚-我遇到的错误--cannotuse*term(typeelastic.AggregationBucketKeyItem)astypeelastic.AggregationsinargumenttoextractBucket产生错误的行是"Value":extractBucket(parts[1:],*term),相关代码,用于上下文//fromhttps://github.com/olivere/elastic/blob/v3.0.22/search_aggs.gotypeAggregationsmap[string]*json.RawMe

xml - 从结构编码 xml

我刚开始尝试让下面的代码正常工作,但运气不好。看起来我没有正确编码结构部分的结构。帮助!packagemainimport("encoding/xml""fmt""os")funcmain(){typePersonstruct{Emailstring`xml:"email"`Phonestring`xml:"phone"`}typeHoststruct{Hostnamestring`xml:"hostname"`Addressstring`xml:"address"`}typeAssetstruct{personPersonhostHost}p:=&Person{Email:"pers

go - 如何将请求主体解码/解码为 Golang 结构和结构 slice ?

我如何在Golang中正确解码它?{"symbol":"ZVZZT.O","params":[{"forward":0,"period":3,"ref":"high","indicator":"sma","freq":"day"},{"forward":1,"period":8,"ref":"close","indicator":"ema","freq":"week"}]}进入这些结构typeIteration4RequestBodystruct{Symbolstring`json:"symbol"`Params[]Iteration4Params`json:"params"`}typ

arrays - golang中另一个结构内的结构数组

这个问题在这里已经有了答案:JSONanddealingwithunexportedfields(2个答案)关闭4年前。我是golang的新手,正在尝试使用golang创建这种格式的json{"Title":"Youareawesome","Url":"www.youareawesome.com","Desc":"yourawesomedescishere","Payment":{"Discount":"15%","outlets":[{"Location":"nowhere"},{"Location":"everywhere"}]}}下面是我的struct代码typePartners