草庐IT

json - 在 Go 中从 JSON 文件动态定义结构

我想在基于JSON文件的Go项目中动态定义结构。例如,如果我有一个像这样的json文件...{"date":"today","time":12,"era":"never","alive":true}然后我希望生成一个结构(看起来)像这样(但未在源代码中明确定义)...typeDynamicJSONstruct{date,erastringtimeintalivebool}此外,我想嵌套JSON对象,这样我就可以做这样的事情......{"date":"today","time":12,"era":"never","alive":true,"nested":{"date":"tomorr

arrays - 无法将字节 slice 解码回结构

我正在尝试读取字节数组并将其输出到Go中的结构中。officialexample是一个很好的起点,但这只会解码单个float64。这othersnippet表明它绝对可以用结构来完成。我的play,但是,失败并返回binary.Read:invalidtype...。我认为这与只接受固定长度数据读入的Read函数有关:binary.Readreadsstructuredbinarydatafromrintodata.Datamustbeapointertoafixed-sizevalueorasliceoffixed-sizevalues这就是为什么我的struct定义只包含固定长度的

go - 删除结构中 slice 的元素

这个问题在这里已经有了答案:Howtochangestructvariablecontent?(1个回答)Whycan'tIappendtoaslicethat'sthepropertyofastructingolang?(1个回答)关闭5年前。我有一个结构“Guest”,其中包含聚会客人的元数据(唯一ID、姓名、姓氏和作为该客人friend的客人的唯一ID列表。typeGueststruct{idintnamestringsurnamestringfriends[]int}我有以下代码可以从friend列表中删除一个ID:func(selfGuest)removeFriend(idi

go - 将 yaml 文件解析为 go 中的预定义结构

我有多个需要解析且结构完全相同的yaml文件schema:"1.0.0"id:testversion:"1.2.3"dependency:-name:uitype:runnercwd:/uiinstall:-name:apigroup:testproperties:name:appurl:appUrl-name:backendtype:mongoDbpath:beinstall:-name:dbtype:mongoprovides:-name:apiproperties:url:urlTheschemasectionismandatoryforalltheyamlwhichtheapp

go - 问题解析yaml文件

我有以下结构,在解析yaml文件后填充问题是引用丢失了Filein.yaml例如_schema:"3.0.0"bar:-oneFileout.yaml_schema:3.0.0bar:-one如你所见,我得到的是3.0.0而不是“3.0.0”,知道如何克服这个问题吗这是我创建的一个小程序来演示这个问题packagemainimport("gopkg.in/yaml.v2""io/ioutil")typeConfigstruct{Schemastring`yaml:"_schema"`Bar[]string}funcmain(){cfg:=Config{}source,err:=iout

generics - 如何使结构字段成为泛型类型

我正在导入两个(以及更多)外部库,它们在功能上相关,但彼此不同。我希望能够执行以下操作:为每个创建一个新对象将每个分配给嵌套结构的一个字段能够通过使用嵌套结构的字段访问库方法大致是这样的:import("github.com/bittrex_api_wrapper""github.com/kraken_api_wrapper")typeExchangestruct{bittrex*datakraken*data}typedatastruct{cntrintapi????}funcInitialize()Exchange{eExchangebrex:=bittrex_api_wrappe

html - 如何在html文件golang中获取这个值

在HTML文件中,我想获取ID和用户名,但该怎么做?因为我得到的总是空白页。//DataHandlerstructtypeDataHandlerstruct{SessionDataHandlersession.SessionData}//HomeHandlerfunctionfuncHomeHandler(whttp.ResponseWriter,r*http.Request){sessionData:=session.GetSession(w,r)data:=DataHandler{SessionDataHandler:session.SessionData{ID:sessionDa

csv - 遍历 golang 中的嵌套结构并将值存储在 slice 字符串的 slice 中

我有一个嵌套结构,我需要遍历字段并将其存储在一个字符串slice中。然后,将其输出到csv文件。现在的问题是我手动访问结构中的每个字段并将其存储在slice接口(interface)的slice中,但我的实际代码有100个字段,因此手动调用每个字段没有意义。此外,在将slice接口(interface)slice存储到csv时遇到问题,因为在写入输出为[][]interface{}的csv文件时出现以下错误//for_,value:=rangeoutput{//err:=writer.Write(value)//ERROR:can'tusevalue(type[]interface{}

go - 在 Go 中访问结构属性

这个问题在这里已经有了答案:Howtocloneastructurewithunexportedfield?(1个回答)关闭4年前。现在我已经实现了这个接口(interface)的结构,如下所示typeMyRunnerstruct{pathstring}func(rMyRunner)soSomthing(newPathstring)error{run(path)returnnil}现在我想创建对象,但出现错误MyRunner文字中未导出字段“路径”的隐式赋值我是这样做的&run.MyRunnter{”a/b/c/“}有没有Go中没有New关键字的方法?

go - 如何填充作为函数引用传递的接口(interface)片段

我有一个小例子,我尝试在一个函数中填充一个[]Entry(其中Entry是一个接口(interface))slice,当参数是单个Entry时这工作正常,但是当我试图传递一个条目slice时我无法通过指针找到我的方式。packagetempimport("encoding/json"uuid"github.com/satori/go.uuid")typeBaseEntrystruct{IDuuid.UUID}func(entry*BaseEntry)GetID()uuid.UUID{returnentry.ID}typeEntryinterface{GetID()uuid.UUID}f