草庐IT

树形结构

全部标签

go - 编码(marshal)嵌入式结构

我一直在研究嵌入的工作原理。http://play.golang.org/p/oHOim4G1-l当我编码Child结构时,它编码为{}。为什么要这样编码? 最佳答案 你的JSON字典是空的,因为结构的任何字段(或结构中嵌入的任何结构)都不是exported.如果您将字段名称更改为以大写字母开头,那么encoding/json模块将能够看到它们。当然,由于您还有名为Name和Value的方法,因此您需要将它们命名为其他名称以避免冲突。 关于go-编码(marshal)嵌入式结构,我们在S

json - 使用 Go (golang) 如何将数据解码为结构,然后从结构中调用特定字段?

我正在尝试执行API请求以从Steam公共(public)API获取一些信息(这主要是为了学习Go以及学习如何处理Json/API请求)到目前为止我已经得到了这段代码:packagemainimport("encoding/json""fmt""io/ioutil""net/http""strconv")typeSteamAPIstruct{APIKeystring}typeGetAppNewsstruct{AppNewsstruct{AppIdint`json:"appid"`NewsItems[]struct{Gidint`json:"gid"`Titlestring`json:"

encoding - 如何在 Go 中持久化或编码链接数据结构?

这个问题在这里已经有了答案:Mystructuresarenotmarshallingintojson[duplicate](3个答案)关闭7年前。我的目标是拥有一个链接的数据结构,这是一个引用另一个结构的结构,等等,所以我可以将它编码到我的文件系统中,并且在我需要它的时候解码它,所以我恢复整个链接结构,内容相同。例子:我有这些链接结构:typeAstruct{b*B}typeBstruct{c[]C}typeCinterface{}我这样初始化它们:varc0C="foo"varc1C="bar"varb*B=&B{}b.c=make([]C,2)b.c[0]=c0b.c[1]=c1

arrays - 如何在内存中布局结构数组?

typePointstruct{x,yint}vararr[4]Point数组在内存中如何布局?实际物体会并排放置吗[Point[x][y]][Point[x][y]][Point[x][y]][Point[x][y]]或者数组是一个指针数组,对象存储在其他地方,比如Java?[&Point0][&Point1][&Point2][&Point3]堆中的某处:...[Point0[x][y]]...[Point1[x][y]]....[Point3[x][y]]...[Point2[x][y]]此外,make()将如何在内存中布置slice?make([]Point,10)

json - 将 JSON 解码为结构

我的问题很小但非常令人沮丧,因为我似乎无法得到答案。我正在尝试访问来自GoogleScript的响应的JSON部分。在Golang中,我已经设法将它简化为这个map[@type:type.googleapis.com/google.apps.script.v1.ExecutionResponseresult:[{"id":1,"casenumber":"CriminalCase20of2012","datedelivered":"2015-10-22T21:00:00.000Z","judge":"GeorgeMatatiaAbalekaDulu","court":"HighCourt

go - 如何在 json 的接口(interface)中存储不同的结构

http://play.golang.org/p/JJnU5ag234我只能让vA直接工作,如果我想根据我期望的json使用我的vI在其中存储A或B,我得到json:无法将对象解码为main.TA类型的Go值packagemainimport("encoding/json""fmt""strings")typeTinterface{Printer()}typeAstruct{JAstring}func(tA)Printer(){fmt.Print("A")}typeBstruct{JBstring}func(tB)Printer(){fmt.Print("B")}varvA[]Avar

xml - 无法使用 golang 解码 XML,始终为空结构

我试图用golang解码XML,但下面的代码给出了一个空结构有人可以帮忙吗?当我运行下面的代码时,我总是得到{{packet}[]}附上源码:packagemainimport("fmt""encoding/xml"//"io/ioutil")typeFieldstruct{XMLNamexml.Name`xml:"field"`namestring`xml:"name,attr"`shownamegstring`xml:"showname,attr"`fields[]Field}typeProtostruct{XMLNamexml.Name`xml:"proto"`namestrin

json - 将包含动态键的 REST API 返回的 JSON 映射到 Golang 中的结构

我正在从我的Go程序调用RESTAPI,该程序在请求中获取n个酒店ID,并将它们的数据作为JSON返回。当我在请求中传递2个id,1018089108070373346和2017089208070373346时,响应如下所示:{"data":{"1018089108070373346":{"name":"ANiceHotel","success":true},"2017089208070373346":{"name":"AnotherNiceHotel","success":true}}}由于我是Golang的新手,所以我使用了一个JSONGo工具,网址为http://mholt.gi

json - 在 golang 中为 JSON 结构创建接口(interface)

假设我有一个struct,我将json参数数据绑定(bind)到liketypeUserstruct{FirstNamestring`json:"firstName"`}属性FirstName必须大写,以便json值可以绑定(bind)到结构。但我还想创建一个interface来接受任何具有FirstName类属性的struct。由于FirstName已经大写并被占用,我必须为方法命名。typeNameInterfaceinterface{FirstName()string//nopeFirstNameValue()string//maybe?}但是在我所有的jsonstruct上为每

go - 从其他包导入的结构未定义

这是我的菜鸟问题。我的models/model.go中有这个结构packagemodelsimport("time""gopkg.in/mgo.v2/bson")typeHorsestruct{Idbson.ObjectId`bson:"_id,omitempty"`TitlestringDescriptionstringCreatedOntime.TimeCreatorstringVisitsintScoreint}在我的controllers/crud.go中,我尝试使用Horse结构packagecontrollersimport("html/template""log""net