草庐IT

INTERFACE

全部标签

go - 我可以在 Go 中创建不同对象的关联数组吗?

我想将某种类型的实例设置为关联数组中的元素。我应该使用什么类型?varobjects//???//TheconstructorwillreturninstanceoftheIndexControllertypeobjects["IndexController"]=index.Constructor()fmt.Println(objects)我将不胜感激! 最佳答案 Gomap通常是同质的(每个值都是同一类型)。如果你想要每个索引不同的类型,你可以创建一个数组中的所有对象都支持的一些接口(interface)。如果您根本不需要对象支持

types - 我可以传入一个 "Type"作为函数参数吗?

我正在尝试构建一个库,自动将结构类型作为RESTful资源提供服务。这是我设想的调用代码中的样子:packagemainimport("fmt""github.com/sergiotapia/paprika")typeProductstruct{NamestringQuantityint}funcmain(){//YouneedtoattacharesourcebygivingPaprikayourroute,//thestructtypeandoptionallyacustomresourcemanager.paprika.Attach("/products",Product,nil

struct - 在结构或接口(interface)内部执行计算?

我正在构建一个数据以保存到mongodb。我有一个来自这样的API的json响应{coord:{lon:20,lat:30}main:[{"temp":304.15,"pressure":1005,"humidity":74,"temp_min":304.15,"temp_max":304.15}]}在main[0].temp、main[0].temp_min、main[0].temp_max中,值以开尔文为单位。我想在将其保存为mongodb之前将其转换为摄氏温度。我可以像这样制作一个简单的结构:typeItemstruct{TempstringPressureintHumidity

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

xml - 接口(interface)和编码/xml Unmarshal

我有一个soap服务,我正在写反对。soapAPI的一部分用于返回查询结果,我希望提供用于解码信封的基本结构,同时允许开发人员填写encoding/xml将解码到的接口(interface)。typeQueryEnvelopestruct{XMLNamexml.Name`xml:"http://schemas.xmlsoap.org/soap/envelope/Envelope"`Body*QueryBody`xml:"http://schemas.xmlsoap.org/soap/envelope/Body"`}typeQueryBodystruct{QueryResult*Quer

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

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(

json - Go:指向接口(interface){} 的指针丢失底层类型

我正在使用Go中的一些“通用”函数,这些函数在interface{}上运行并在channel周围发送东西,等等。精简一下,假设我有类似的东西:typeMyTypestruct{//Fields}func(m*MyType)MarshalJSON()([]byte,error){//MarshalJSONlog.Print("customJSONmarshal")return[]byte("hello"),nil}funcGenericFunc(vinterface{}){//Dothings...log.Print(reflect.TypeOf(v))log.Print(reflect

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(