草庐IT

Hits_Field

全部标签

go - mgo : result has no field or method 错误

我有以下代码packagemainimport("encoding/json""fmt""labix.org/v2/mgo""labix.org/v2/mgo/bson")funcinsertEntry(j*map[string]interface{},entrystring){err:=json.Unmarshal([]byte(entry),j)iferr!=nil{panic(err)}}funcmain(){c1:=`{"mw":42.0922,"ΔfH°gas":{"value":372.38,"units":"kJ/mol"},"S°gas":{"value":216.81

mongodb - mgo,mongodb : Finding documents that match one field from embedded struct

问题的简化示例你好,使用mgo将文档插入到mongodb中,我试图将一个文档嵌入到另一个文档中。对于mgo,我为此使用了两个结构:typeTeststruct{InTestSubTest`bson:"in_test"`}typeSubTeststruct{Test1string`bson:"test1"`Test2string`bson:"test2"`}然后我插入一个文档:test:=Test{InTest:SubTest{Test1:"test",Test2:"hello"}}err=col.Insert(test)iferr!=nil{fmt.Printf("Can'tinser

转到模板 : Use nested struct's field and {{range}} tag together

我有以下嵌套结构,我想在模板中的{{range.Foos}}标记中迭代它们。typeFoostruct{Field1,Field2string}typeNestedStructstruct{NestedStructIDstringFoos[]Foo}我正在尝试使用以下html/模板,但它无法从NestedStruct访问NestedStructID。{{range.Foos}}{source:'{{.Field1}}',target:'{{.NestedStructID}}'}{{end}}golang模板有什么办法可以做我想做的事吗? 最佳答案

google-app-engine - c.Infof undefined (type context.Context has no field or method Infof) google.golang.org/appengine/log 错误

在GoRuntime中,我使用方法c.Infof来记录消息,但编译失败并出现以下错误c.Infof未定义(类型context.Context没有字段或方法Infof)。错误清楚地表明从c:=appengine.NewContext(r)返回的应用引擎上下文是context.Context类型并且它上面没有方法c.Infof。但与此相反的是https://godoc.org/google.golang.org/appengine/log中的文档表明存在这种方法。还有一点需要注意,该方法存在于“appengine”(导入“appengine”)包返回的上下文中,而这似乎不存在于新包goog

pointers - Go 在哪里定义 p.Field(与 (*p).Field 相对)是有效语法,即使 p 是指向结构的指针?

这是我的Go程序。packagemainimport("fmt")typePersonstruct{Namestring}funcmain(){p:=&Person{"Jack"}//Thefollowingstatementmakessense.Wedereferencethe//pointertoreachthePersonobjectandthenretrieveitsName//field.fmt.Println((*p).Name)//Butthefollowingstatementdoesnotmakesense.Howdoes//thiscodeworkbyretriev

reflection - 如何实现 GetStringValueByField(n interface{}, field_name string) string

我正在尝试实现一种方法,以从任意结构字段中获取值作为结构给出的字符串,并将字段名作为字符串。有了reflect它总是panic。panic:reflect:callofreflect.Value.FieldByNameoninterfaceValuegoroutine16[running]//attempttoimplementGetStringValueByFieldName()packagemainimport"fmt"import"reflect"import"strconv"funcmain(){a:=Order{A:"asdf",B:123}fmt.Println(a)fmt

去生成 : stringer: invalid operation: has no field or method String

我正在尝试使用stringercmd以便我可以为某些int类型生成String()方法。这是代码的样子//go:generatestringer-type=MyIntTypetypeMyIntTypeintconst(resourceMyIntType=iota)funcmyfunc(){print(resource.String())}我在执行gogenerate命令时遇到的错误是invalidoperation:resource(constant0oftypeMyIntType)hasnofieldormethodString这是有道理的,因为还没有String方法。如果strin

Golang Gin "c.Param undefined (type *gin.Context has no field or method Param)"

我尝试使用作为Golang框架的Gin。https://github.com/gin-gonic/gin我从官方github上复制了示例代码。就像这样。packagemainimport("github.com/gin-gonic/gin""net/http")funcmain(){router:=gin.Default()router.GET("/user/:name",func(c*gin.Context){name:=c.Param("name")c.String(http.StatusOK,"Hello%s",name)})router.Run(":8080")}但是我得到了错

去 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