草庐IT

Wmissing-field-initializers

全部标签

戈朗 : loop through fields of a struct modify them and and return the struct?

我正在尝试遍历结构的各个字段,将一个函数应用于每个字段,然后将原始结构作为一个整体返回,并带有修改后的字段值。显然,如果它是一个结构,这不会带来挑战,但我需要函数是动态的。对于这个例子,我引用了Post和Category结构,如下所示typePoststruct{fieldNamedata`check:"value1"...}typePoststruct{fieldNamedata`check:"value2"...}然后我有一个switch函数,它循环遍历结构的各个字段,并根据check的值,将函数应用于该字段的data如下typeDatastoreinterface{...}fun

去 XML 解析 : use attributes as struct field name

如何将XML属性用作结构字段?这是我的测试:每行对应一个人packagemainimport("encoding/xml""fmt")varxmlstr=`John234`typeDatastruct{XMLNamexml.Name`xml:"data"`Person[]Person`xml:"row"`}typePersonstruct{PersonField[]PersonField`xml:"col"`}typePersonFieldstruct{Namestring`xml:"name,attr"`Valuestring`xml:",chardata"`}funcmain(){

json - 为什么struct field的格式串总是小写

当用json编码/解码结构时,几乎所有的代码都使用相同的字段名,但首字母小写,这是为什么?既然名称相同,而且json肯定可以处理任何情况,为什么要添加这个重复的东西:Namestring`json:"name"`为什么不直接使用Namestring?在其他情况下,如果名称与go字段名称不同,则添加格式字符串是有意义的:Namestring`json:"MyName"` 最佳答案 encoding/jsondocumentation说:Theencodingofeachstructfieldcanbecustomizedbythefo

reflection - 去(反射): How to Instantiate an arbitrary type and set a known embedded field

考虑以下类型声明:type(Embeddedstruct{}Actual1struct{*Embedded}Actual2struct{*Embedded}Actual3struct{*Embedded})现在考虑以下函数,其中i可能是Actual1、Actual2或Actual3类型(或以类似方式嵌入Embedded的任何其他类型)。我无法进行类型断言或类型切换,因为我不知道有多少类型包含Embedded,关于i我所知道的就是它确实嵌入了嵌入式类型。此函数将实例化一个与i类型相同的新实例,并在该新实例化的副本实例上设置embed。funcNew(iinterface{},field*

xml - 在 Golang 中编码 XML : field is empty (APPEND doesn't work? )

我正在学习用Go创建XML。这是我的代码:typeRequeststruct{XMLNamexml.Name`xml:"request"`Actionstring`xml:"action,attr"`...Point[]point`xml:"point,omitempty"`}typepointstruct{geostring`xml:"point"`radiusint`xml:"radius,attr"`}funcmain(){v:=&Request{Action:"get-objects"}v.Point=append(v.Point,point{geo:"55.703038,37

戈朗/mgo : leave out empty fields from insert

出于某种原因,即使我设置了omitempty选项,mgo仍将空结构作为空值插入到数据库中。packagemainimport("fmt""encoding/json")typeAstruct{Abool}typeBstruct{Xint`json:"x,omitempty"bson:"x,omitempty"`SomeA*A`json:"a,omitempty"bson:"a,omitempty"`}funcmain(){b:=B{}b.X=123ifbuf,err:=json.MarshalIndent(&b,"","");err!=nil{fmt.Println(err)}else

go - golang 中的 make 和 initialize struct 有什么区别?

我们可以通过make函数创建channel,通过{}表达式新建一个对象。ch:=make(chaninterface{})o:=struct{}{}但是,make和{}新建map有什么区别?m0:=make(map[int]int)m1:=map[int]int{} 最佳答案 make可用于使用预分配空间初始化映射。它需要一个可选的第二个参数。m0:=make(map[int]int,1000)//为1000个条目分配空间分配需要cpu时间。如果您知道映射中将有多少个条目,您可以为所有条目预分配空间。这减少了执行时间。您可以运行以下

戈朗 : How can i access json decoded field?

我有下一个JSON数据:http://jsonblob.com/532d537ce4b0f2fd20c517a4所以我试图迭代的(就像PHP中的foreach一样)是:invoices->invoice(是一个数组)所以,我想做的是:packagemainimport("fmt""reflect""encoding/json")funcmain(){json_string:=`{"result":"success","totalresults":"494","startnumber":0,"numreturned":2,"invoices":{"invoice":[{"id":"106

initialization - Go if 语句中的多个初始值设定项

刚刚发现Go,到目前为止我非常好奇。我知道我只是懒惰,但我想知道是否可以在if语句中初始化多个变量。我知道以下是可能的:ifx:=5;x==5{fmt.Printf("Whee!\n")}我试过以下方法:ifx:=5,y:=38;x==5{fmt.Printf("Whee!%d\n",y)}ifx:=5&&y:=38;x==5{fmt.Printf("Whee!%d\n",y)}但都没有用。我查看了Go网站上的文档,是否有任何遗漏或这根本不可能? 最佳答案 方法如下:packagemainimport("fmt")funcmain(

linux - 海湾合作委员会错误 : 'for' loop initial declaration used outside C99 mode

当我尝试使用make进行编译时,我遇到了error:'for'loopinitialdeclarationusedoutsideC99mode。我找到了一个wiki就是说Put-std=c99inthecompilationline:gcc-std=c99foo.c-ofoo问题是我不知道如何在make中指定它。我打开Makefile,找到CC=gcc并将其更改为CC=gcc-std=c99,但没有结果。有什么想法吗? 最佳答案 将CFLAGS=-std=c99放在Makefile的顶部。要在不使用C99的情况下消除错误,您只需在f