草庐IT

文章结构

全部标签

json - 如何将 map[string]interface{} 转换为不同类型的结构?

我正在调用一个API,它将像这样返回Json对象:{name:"XXX"type:"TYPE_1"shared_fields:{...}type_1_fields:{...}..type_2_fields:{...}}根据不同的类型,这个对象会有不同种类的字段,但是这些字段对于不同的类型是一定的。因此,我将Json字符串解码为map[string]interface{}以获取不同的类型,但是如何将这些map[string]interface{}转换为某个结构?varfmap[string]interface{}err:=json.Unmarshal(b,&f)type:=f["type

reflection - 使用(相对)未知/任意方法扩展结构,进行反射(或避免反射)

下面显然不起作用:Arbitrary:=struct{field1stringfield2string}{"a","b"}fmap:=make(map[string]func(string)string)fmap["fone"]=func(sstring)string{fmt.Printf("functionfone:%s",s)}fmap["ftwo"]=func(sstring)string{fmt.Printf("functionftwo:%s",s)}//probablyok,assimpleexamplesgo,tothispointwherereflectionneedst

go - 范围超过结构数组的地址

我有一个[]Struct类型的结构数组。当我以以下形式覆盖它时:fori,val:=rangemystructarray我知道val是一个局部变量,它包含mystructarray[i]的副本。有没有比这更好的遍历mystructarray的地址的方法:fori:=rangemystructarray{valptr=&mystructarray[i]}? 最佳答案 在接收到slice内容的指针时无法进行迭代(当然,除非它是一个指针slice)。你的例子是最好的方式:fori:=rangemySlice{x=&mySlice[i]//

interface - 我如何在 Go 中将 interface{} 的一部分转换为我的结构类型的一部分?

这个问题在这里已经有了答案:Typeconvertingslicesofinterfaces(9个回答)关闭3年前。funcGetFromDB(tableNamestring,m*bson.M)interface{}{var(__session*mgo.Session=getSession())//ifthequeryargisnil.giveitthenullqueryifm==nil{m=&bson.M{}}__result:=[]interface{}{}__cs_Group:=__session.DB(T_dbName).C(tableName)__cs_Group.Find(

Go:将空结构编码为 json

我正在尝试将结构编码为json。它在结构具有值时起作用。但是,当结构没有值时,我无法访问网页:开始:typeFruitsstruct{Apple[]*Description'json:"apple,omitempty"'}typeDescriptionstruct{ColorstringWeightint}funcHandler(whttp.ResponseWriter,r*http.Request){j:={[]}js,_:=json.Marshal(j)w.Write(js)}错误是因为json.Marshal无法编码空结构吗? 最佳答案

file - 在 Go 中将多个结构写入一个文件

我和我的团队是Go的新手,我们有一个“Header”结构和多个我们试图写入文件的“Record”结构。但是,每当我们尝试通过重写来更新文件中的Header结构时,文件的其余部分就会变得一团糟。我们正在使用编码/解码:(数据文件从os.Open返回)dataFile.Seek(header.FreePtr,0)//seektofreespace-couldwejustrefactorandseektoendoffile?encoder:=gob.NewEncoder((dataFile))err=encoder.Encode(record)iferr!=nil{panic(err)}da

pointers - Golang 结构问题指向父结构方法

我在Golang中遇到了如下问题:packagemainimport"fmt"typeFoostruct{namestring}typeBarstruct{Fooidstring}func(f*Foo)SetName(namestring){f.name=name}func(f*Foo)Name()string{returnf.name}funcmain(){f:=&Foo{}f.SetName("SetFooname")fmt.Println("GetfromFoostructname:",f.Name())bar:=&Bar{Foo:Foo{name:"SetFoonamefrom

mongodb - 无法使用 golang 将结构保存到 mongodb 中(仅创建空记录)

我有以下结构typeResultstruct{nidstringtimestampint64hexhashstringaddrstring}我想保存到mongodb中:我创造了它r:=Result{hex_id,int64(msg.timestamp.Unix()),hexhash,msg.addr.String()}并测试是否正确创建:fmt.Println(r)这给了我预期的结果:{b8da3f19d1318af6879976c1eea66c78c48e1144142141725265072917F19D7F4C4B54C9C66A3EB31F77012981127.0.0.1:6

database - 如何从 interface{} 值(反射)为显式类型的结构成员设置新值?戈朗

我想了解使用反射包的一些微妙时刻。请看下面的示例,它更好地描述了我想知道的内容:typeRobotstruct{idintmodelstring}funcchange(iinterface{},fields...string){v:=reflect.ValueOf(i).Elem()//hereIemulatefunctionbyslicethatcouldreturnanyvalue,//sohereIneedtocheckifIcanstoreincomingvaluestoexistingstructreturns:=[]interface{}{100,"Something"}f

go - 将结构实例上的方法作为参数传递

我有一个接受函数作为参数的函数:funcsend(nint,cfunc(xint)int)int{returnc(n)}我有一个结构,上面定义了一个方法typedatastruct{valueint}func(t*data)set(xint){t.value=x}我想创建一个结构实例,并将绑定(bind)到该实例的方法set作为第二个参数传递给send函数,以设置来自send的value字段。这可能吗?https://play.golang.org/p/bv1JevQBcq 最佳答案 您可以使用methodvalue.这是类似于您的