草庐IT

multiple-interface-implem

全部标签

go - 无法使用 Go 在函数中覆盖列表类型接口(interface) {}

不能使用Go在函数中覆盖列表类型接口(interface){}。对我来说很重要,然后我在一个函数中执行。如何修复?packagemainimport("fmt")typeMyBoxItemstruct{Namestring}typeMyBoxstruct{Items[]MyBoxItem}func(box*MyBox)AddItem(itemMyBoxItem)[]MyBoxItem{box.Items=append(box.Items,item)returnbox.Items}funcPrintCustomArray(listinterface{})interface{}{//ite

go - 自身类型的接口(interface)方法返回值

我正在尝试制作一种方法,该方法将采用某种类型的结构并对它们进行操作。但是,我需要有一个可以调用结构实例的方法,它将返回该结构类型的对象。我收到编译时错误,因为实现接口(interface)的类型的返回类型与接口(interface)的方法返回类型不同,但那是因为接口(interface)需要返回它自己类型的值。接口(interface)声明:typeGraphNodeinterface{Children()[]GraphNodeIsGoal()boolGetParent()GraphNodeSetParent(GraphNode)GraphNodeGetDepth()float64Ke

go - 自身类型的接口(interface)方法返回值

我正在尝试制作一种方法,该方法将采用某种类型的结构并对它们进行操作。但是,我需要有一个可以调用结构实例的方法,它将返回该结构类型的对象。我收到编译时错误,因为实现接口(interface)的类型的返回类型与接口(interface)的方法返回类型不同,但那是因为接口(interface)需要返回它自己类型的值。接口(interface)声明:typeGraphNodeinterface{Children()[]GraphNodeIsGoal()boolGetParent()GraphNodeSetParent(GraphNode)GraphNodeGetDepth()float64Ke

go - 如何返回指定返回值的子类型(在本例中为 interface{})?

我有一个接口(interface),它定义了一个类型为func(interface{},proto.Message)interface{}的参数,我正在尝试传递类型为funcreduceMsg(ainterface{},bproto.Message)[]*PersistentData给它。这会导致以下编译器错误:CannotusereduceMsg(typefunc(ainterface{},bproto.Message)[]*PersistentDataastypefunc(interface{},proto.Message)interface{}此错误的原因是什么,我该如何解决?似

go - 如何返回指定返回值的子类型(在本例中为 interface{})?

我有一个接口(interface),它定义了一个类型为func(interface{},proto.Message)interface{}的参数,我正在尝试传递类型为funcreduceMsg(ainterface{},bproto.Message)[]*PersistentData给它。这会导致以下编译器错误:CannotusereduceMsg(typefunc(ainterface{},bproto.Message)[]*PersistentDataastypefunc(interface{},proto.Message)interface{}此错误的原因是什么,我该如何解决?似

go - 为什么golang结构数组不能分配给接口(interface)数组

我正在努力实现以下目标。packagemainimport("fmt")typeMyStructstruct{Valueint}funcmain(){x:=[]MyStruct{MyStruct{Value:5,},MyStruct{Value:6,},}vary[]interface{}y=x//Thisthrowsacompiletimeerror_,_=x,y}这给出了一个编译时错误:sample.go:21:cannotusex(type[]MyStruct)astype[]interface{}inassignment为什么这不可能?如果不行,在Golang中有没有其他方法来

go - 为什么golang结构数组不能分配给接口(interface)数组

我正在努力实现以下目标。packagemainimport("fmt")typeMyStructstruct{Valueint}funcmain(){x:=[]MyStruct{MyStruct{Value:5,},MyStruct{Value:6,},}vary[]interface{}y=x//Thisthrowsacompiletimeerror_,_=x,y}这给出了一个编译时错误:sample.go:21:cannotusex(type[]MyStruct)astype[]interface{}inassignment为什么这不可能?如果不行,在Golang中有没有其他方法来

arrays - 实现Scan接口(interface)将json数组读入map

我正在从PostgreSQL获取一个JSON数组,我想将它读入map。我能够将值解码到[]stringslice中,但我真正想要的是map[string]bool。我为带有Scan接口(interface)的列编写了一个自定义类型,该接口(interface)首先将JSON数组转换为一段字符串,然后将每个字符串作为键读入自定义映射类型。typecustMapmap[string]boolfunc(m*custMap)Scan(srcinterface{})error{b,ok:=src.([]byte)if!ok{returnerror(errors.New("ErrorScannin

arrays - 实现Scan接口(interface)将json数组读入map

我正在从PostgreSQL获取一个JSON数组,我想将它读入map。我能够将值解码到[]stringslice中,但我真正想要的是map[string]bool。我为带有Scan接口(interface)的列编写了一个自定义类型,该接口(interface)首先将JSON数组转换为一段字符串,然后将每个字符串作为键读入自定义映射类型。typecustMapmap[string]boolfunc(m*custMap)Scan(srcinterface{})error{b,ok:=src.([]byte)if!ok{returnerror(errors.New("ErrorScannin

golang 检查 slice 的长度,如果它是一个 slice map[string]interface{}

我想看看v的类型是不是slice。如果是这样,我想检查它的长度。vara=make(map[string]interface{})a["a"]=1a["b"]=[]string{"abc","def"}a["c"]=[]int{1,2,3}fork,v:=rangea{ifreflect.TypeOf(v).Kind()==reflect.Slice{t.Log("Lengthofmap",k,len(v))//invalidargumentv(typeinterface{})forlen}}既然我知道它是一个slice,我该如何检查它的长度?预期输出:Lengthofmapb2Len