草庐IT

GL_REFLECTION_MAP

全部标签

algorithm - map 和动态规划更新

给我的问题是一个child正跑上n级楼梯,一次可以跳1步、2步或3步。实现一种方法来计算child可以通过多少种可能的方式跑上楼梯。http://play.golang.org/p/bpjIkMm9jHpackagemainimport"fmt"funcCountWaysDP(nint,mmmap[int]int)int{ifn-1{returnmm[n]}else{mm[n]=CountWaysDP(n-1,mm)+CountWaysDP(n-2,mm)+CountWaysDP(n-3,mm)returnmm[n]}}funcmain(){mm:=make(map[int]int)f

reflection - 设置作为接口(interface)传递的任何结构的变量{}

我想知道如何在使用interface{}值时使用反射设置变量,并且所有类型的结构都可以传递给funcF(ointerface{})。如何将第一个值(s.A)更改为'hello'?packagemainimport("fmt""reflect")typeTstruct{Astring}funcmain(){F(T{"foo"})}funcF(ointerface{}){t:=reflect.ValueOf(&T{"bar"}).Elem()s:=reflect.ValueOf(&o).Elem()//okfmt.Println("struct:",t.Field(0).CanSet())

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

戈朗 : How can I populate a multi-struct map in a loop?

我有客户与API交互的日志文件。我想解析这些日志并将结果提供给结构映射,以便我可以将数据组织成有用的信息。例如,我想响应以下查询:“显示每个用户每天的请求总数”。我已经创建了一个看起来足够的结构来保存数据。但是,当我尝试运行程序时出现错误:无效操作:dates[fields[1]](type*Dates不支持索引)[processexitedwithnon-zerostatus]。http://play.golang.org/p/8u3jX26kttpackagemainimport("fmt""strings")typeStatsstruct{totalNumberOfRequest

dictionary - 迭代并从 map 中获取值

我有一些JSON数据,我已将其解编到名为data_json的map中。它包含数百个项目。使用以下代码,我可以成功检索map中其中一项的“dn”值,但是我正在努力如何遍历整个结构以获取map中所有项目的“dn”值map。objects:=data_json["data"].([]interface{})first:=objects[0].(map[string]interface{})fmt.Println(first["dn"])我尝试过这种方法,但我对应该如何构造键和值感到困惑。forv,k:=rangekeys{fmt.Println("Key:",k,"Value:",m[k])

arrays - 如何从 go map 对象访问深度嵌入的 json 对象?

我是新手,我了解如何将json数据编码为自定义预定义结构类型,但我目前正在使用一个JSON集,它可以在每次调用时具有动态键和值。我可以将这些动态数据编码到map/界面中,没问题,但我对如何访问深度嵌套在数组中的项目有点迷茫。这是我在USDOL网站上使用的示例JSON{"name":"osha-establishment","count":15,"frequency":"ManualCrawl","version":4,"newdata":true,"lastrunstatus":"success","lastsuccess":"MonDec08201411:19:57GMT+0000(

reflection - 如何使用 reflect 遍历结构

我有一个包含一些url参数的特定结构,我想使用reflect构建一个url参数字符串以遍历结构字段,这样我就不会关心结构真正包含什么。假设我有一个这样的结构:typeStudentstruct{Namestring`paramName:"username"`Ageint`paramName:userage`}我这样分配一个学生:s:=Student{Name:"Bob",Age:15,}我想为这个学生实例构建一个这样的查询参数字符串:username=Bob&userage=15到目前为止我有:func(sStudent)buildParams()string{st:=reflect.

json - 如何在 go lang 中对 map[string]interface{} 类型进行多重排序?

场景:假设我有一个JSON数据要在golang中处理现在我正在使用map[string]interface{}类型,通过执行marshal/unmarshal使用packageencoding/json下面是JSON数据:{"MysoreCity":{"Population":1000,"VehicleCount":1700,"Temperature":33},"BangaloreCity":{"Population":1000,"VehicleCount":3500,"Temperature":33},"KolarCity":{"Population":1250,"VehicleCo

json - 如何以 json 格式显示 map 中的所有数据 - Golang?

我正在golang中创建一个API,它将简单地以json格式显示map中的所有数据。端点:/keystypeUserControllerstruct{}//NewUserControllerfunctionfuncNewUserController()*UserController{return&UserController{}}//DatastructtypeDatastruct{Datakeyint`json:"key"`Datavaluestring`json:"value"`}vardatamap=make(map[int]string)func(ucUserControlle