草庐IT

接口封装

全部标签

go - map 范围循环中的空接口(interface)

下面的代码没有按预期工作。packagemainimport"fmt"funcmain(){questions:=make(map[int]interface{})questions[1]=map[interface{}]string{"q1":"ThisisQuestion-1?","op1":"ThisisOption-1","op2":"ThisisOption-2",true:"ThisisOption-1",}//Thisgivemap[interface{}]stringfmt.Printf("%T\n",questions[1])//Thisnotworkingforke

go - 如何将多维 slice []接口(interface){}转换为 slice []字符串?

示例来源://sourcemultidimensionalslicevarsource=[]interface{}{"value1","value2",1234,1234.1234,[]int{222,333},[]float32{444.444,555.555},[]interface{}{555,"value4",[]int{777,888}}}目标://target[]stringvartarget=[]string{"value1","value2","1234","1234.1234","222","333","444.444","555.555","555","value4

go - 带有接口(interface)参数不兼容错误的类型函数

我已经声明了一个新类型func,它采用符合interface{}的任何值。但是,当我调用一个作为参数传递的函数(符合该类型规范)时,我得到一个错误。有人能解释一下为什么会这样吗?下面是我可以重现问题的最简单示例。typemyfuncfunc(xinterface{})funca(numint){return}funcb(fmyfunc){f(2)return}funcmain(){b(a)//error:cannotusea(typefunc(int))astypemyfuncinargumenttobreturn} 最佳答案 您在

go - 即使接口(interface)相同,也不能将类型 X 用作类型 Y

我正在尝试编写一个函数,它将实现特定接口(interface)的任何东西作为参数。我已经定义了一个接口(interface)KeyProvider,它指定了一个GetKey()方法。我已经定义了一个使用此接口(interface)ToKeys()的函数。typeKeyProviderinterface{GetKey()*datastore.Key}funcToKeys(l[]*KeyProvider)[]*datastore.Key{keys:=make([]*datastore.Key,len(l))fori,vp:=rangel{v:=*vpkeys[i]=v.GetKey()}r

go - 等效结构上字段的接口(interface)转换

有没有一种直接的方法可以将某些字段为“通用”(接口(interface){})的结构转换为另一种具有相同字段名称但“强类型”(>int,string,等等)?让我们说,给定定义:packagemainimport("fmt")typeGenericDatastruct{HardintSoftinterface{}}typeDatastruct{HardintSoftint}typeGenericDataGeneratorfunc()GenericDatafuncgenerateGenericData()interface{}{returnGenericData{1,2}}funcret

Golang 接口(interface)方法链接

我有一个接口(interface)Cells有几个方法typeCellsinterface{Len()int//....}具体实现有StrCells、IntCells、FloatCells和BoolCells,它们都有上面的实现的方法。例如:typeStrCells[]stringfunc(sCStrCells)Len()int{returnlen(sC)}//...typeIntCells[]intfunc(iCIntCells)Len()int{returnlen(iC)}//...//....对于两种具体类型-IntCells和FloatCells-我想实现仅适用于这些类型的特定

go - 从字符串列表转换为 go 中的接口(interface)列表

我想在go中传递一个字符串列表作为通用参数,但不确定是否可行。我有变通办法,但感觉我只是无法获得正确的语法。packagemainimport"fmt"funcSet(otherFields...interface{}){fmt.Printf("%v",otherFields)}funcmain(){a:=[]string{"Abc","def","ghi"}Set(a)//incorrectbehaviorbecauseapassedthroughasalist,ratherthanabunchofparameters//Set(a...)//compilererror:cannot

go - 创建结构时正确使用接口(interface)

我正在尝试编写一个小程序,其中有几个包,每个包都有一个实现接口(interface)的结构。我的想法是,基于用户输入,我可以选择使用哪个包来构建特定结构,然后在其上调用它们都应该具有的函数。由于我事先不知道类型,我的印象是我可以使用interface{}并将其用作前向声明(请参阅最后一个代码片段)。我有一些看起来像这样的东西:packagefootypeFooInputstruct{BarstringBazint}typeFoointerface{Ding()Dong()}在另一个包中,bob,我有类似的东西:typeBobstruct{foo.FooInput}func(mybob*

go - 如何通过 Golang 中的函数传递接口(interface)指针?

我在测试golang功能时遇到了这个概念,我可以将接口(interface)的指针用作接口(interface)本身。在下面的代码中,如何确保one的值更改为random。packagemainimport("fmt")funcsome(checkinterface{}){check="random"}funcmain(){varone*interface{}some(one)fmt.Println(one)}具体来说,我需要将接口(interface)指针传递给接受接口(interface)作为参数的函数的方法。谢谢! 最佳答案

json - 将[][]接口(interface)转换为[][]字符串

这个问题在这里已经有了答案:Cannotconvert[]stringto[]interface{}(7个答案)关闭5年前。有什么简单的方法可以将[][]interface{}转换为[][]string吗?我想要的是将此[][]接口(interface){}写入csv但writer在go只接受[][]string。附加信息:我的[][]接口(interface){}包含4列,其中2列是字符串,2列是json.Number。提前致谢。