草庐IT

Interface1

全部标签

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-我想实现仅适用于这些类型的特定

golang 默认将 json 对象解码为 map[string]interface{},如何将其解码为 []byte?

默认情况下,golang将json对象解码为map[string]interface{},如何将其解码为[]byte?因为我需要在获得其类型后将其二次解码为结构实例。 最佳答案 为什么不直接将json解码到结构中?或者如果你有更多的对象到结构的slice中?packagemainimport("encoding/json""fmt")typeTestJsonstruct{FoostringBazstring}var(jsonValue=`{"FOO":"BAR","BAZ":"QUX"}`jsonValueSlice=`[{"FOO

json - golang 无法反射(reflect)到 map[interface{}]interface{}

我原来的问题是我想解析URL.Values到通用类型(map[interface{}]interface{})编辑/添加一些值,然后将其转换为JSON字符串并将其放入PostgreSQLJSON列。我尝试用这段代码来解析它,但是content似乎是null而err是false。request.URL.Query()打印一个漂亮的map对象。v:=reflect.ValueOf(request.URL.Query())i:=v.Interface()content,err:=i.(map[interface{}]interface{})//DosomeoperationsjsonStri

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。提前致谢。