草庐IT

zval_struct

全部标签

json - Go struct 表示 twitter JSON 结果

我已经在下面对这个Gotwitter客户端进行了改进,客户端在显示结果方面仍然需要一些工作,我想表示JSON结果http://pastie.org/7298856作为Go结构,我不需要JSON结果中的所有字段,任何指针?packagemainimport("fmt""io/ioutil""log""net/http")typeTwitterResultstruct{}vartwitterUrl="http://search.twitter.com/search.json?q=%23KOT"funcretrieveTweets(cchan 最佳答案

去基础 : What is the diference between calling a method on struct and calling it on a pointer to that struct?

假设我有一个Vertex类型typeVertexstruct{X,Yfloat64}我已经定义了一个方法func(v*Vertex)Abs()float64{returnmath.Sqrt(v.X*v.X+v.Y*v.Y)}这两个调用有什么区别?(两者返回相同的结果)v1:=Vertex{3,4}fmt.Println(v1.Abs())v2:=&Vertex{3,4}fmt.Println(v2.Abs()) 最佳答案 第一个版本相当于varv1Vertexv1.X=3v1.y=4fmt.Println((&v1).Abs)第二个

struct - MGO/GOLANG : Struct to unmarshall a document with

我有一个如下所示的Mongo模式:varphoneBookSchema=Schema({user_id:{type:Schema.Types.ObjectId,ref:'User',index:{unique:true},required:true},entries:{type:[entry],default:[]},matches:{type:[],default:[]}});条目文档数组如下所示:varentry=Schema({_id:false,phone:{type:String,index:true},name:{type:String},notified:{type:Bo

json - 检查值是否为 typeof struct

我想在Context上实现一个Send方法,它将给定的对象写入http.ResponseWriter。目前我有:packagecontextimport("net/http")typeContextstruct{Request*http.RequestResponseWriterhttp.ResponseWriter}typeHandlefunc(*Context)funcNew(whttp.ResponseWriter,r*http.Request)*Context{return&Context{r,w}}func(c*Context)Send(valueinterface{},co

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

go - 如何使用作为接口(interface)参数传入的 Struct 来操作解码的 XML

有没有办法通过作为接口(interface)参数传入的结构来操作解码的XML?我将结构作为具有接口(interface)类型的参数传递,但在使用xml.Decode对其进行解码后,我无法指定其字段以将其字段检索为结构。我认为go是在提示它不知道要查找哪个结构,除非特别提及。下面是一个示例函数:funcUpdateEntity(response*restful.Response,xml_templatestring,dataStructinterface{},respStructinterface{})(*restful.Response,interface{}){payload:=re

转到模板 : 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模板有什么办法可以做我想做的事吗? 最佳答案

go - 在go中导入struct,得到 "not a type"错误

我导入了另一个包中定义的结构,当尝试使用它来构造文字时,出现“不是类型”错误。在publish.go中typeBookstruct{NamestringAuthorstringPublishedbool}在商店.goimport"publish"funcInit(){varreadingpublish.Bookb:=&reading{Name:"LearnGoLang",Author:"Rob",Published:true}}错误:阅读不是一种类型 最佳答案 在这里你尝试创建一个类型为“reading”的结构体b:=&readin

go - 是否可以扩展 go struct 构造函数?

给定typeRectanglestruct{h,wint}func(rec*Rectangle)area()int{returnrec.w*rec.h}你能用Rectangle定义一个Square结构,这样我就可以使用area方法了吗?如果不可能,那绝对没问题。我不会评判语言,不会哭泣或生气。我刚学golang。 最佳答案 Go不是经典的面向对象的,因此它没有继承。它也没有构造函数。它所具有的是嵌入。因此这是可能的:typeRectanglestruct{h,wint}func(rec*Rectangle)area()int{ret

struct - 为什么将结构传递给当前包中带有文字结构参数的函数不同于另一个包中的函数?

这很有效:packagemainimport"fmt"typeStructstruct{fieldstring}funcFn(argstruct{fieldstring}){fmt.Println(arg)}funcmain(){Fn(Struct{"john"})}但这给出了./main.go:12:cannotuseStructliteral(typeStruct)astypestruct{fieldstring}inargumenttosub.Fn:main.gopackagemainimport"go_tests/sub"typeStructstruct{fieldstring