草庐IT

interface-design

全部标签

go - 将 JSON 整数解码为空接口(interface)会导致错误的类型断言

我有这个代码。我希望接口(interface)的类型断言为int。但是,接口(interface)的类型改为float64。谁能解释为什么会这样?规避它的最佳方法是什么。packagemainimport("fmt""encoding/json")typeobjstruct{Xinterface{}}funcmain(){varxintx=5o:=&obj{X:x,}b,_:=json.Marshal(o)varnewObjobjjson.Unmarshal(b,&newObj)if_,ok:=newObj.X.(int);ok{fmt.Println("Xisanint")}else

golang 初始化一片空接口(interface)

这个问题在这里已经有了答案:Cannotconvert[]stringto[]interface{}(7个答案)关闭5年前。我定义了一个结构typecontainerstruct{Data[]interface{}}并希望将各种不同数据类型的slice分配给它。例如ints:=[]int{2,3,4}tmp:=container{ints}但是,编译器会报错:cannotuseints(type[]int)astype[]interface{}infieldvalue我应该如何定义容器结构?或者作业需要以不同的方式完成?可以找到一个完整的例子here

golang 初始化一片空接口(interface)

这个问题在这里已经有了答案:Cannotconvert[]stringto[]interface{}(7个答案)关闭5年前。我定义了一个结构typecontainerstruct{Data[]interface{}}并希望将各种不同数据类型的slice分配给它。例如ints:=[]int{2,3,4}tmp:=container{ints}但是,编译器会报错:cannotuseints(type[]int)astype[]interface{}infieldvalue我应该如何定义容器结构?或者作业需要以不同的方式完成?可以找到一个完整的例子here

pointers - 如果参数的类型是 interface{} 你怎么知道你是按指针传递还是按值传递?

给定任何采用interface{}类型参数的函数,我如何知道是否传递带有或不带有&的参数,而无需浏览源代码函数。例如,如果我有一个具有此类型签名的函数:funcfoo(xinterface{},yint)int是否有任何方法可以确定x应该按值还是按指针传递? 最佳答案 这是来自源代码的片段://DecodeElementworkslikeUnmarshalexceptthatittakes//apointertothestartXMLelementtodecodeintov.//Itisusefulwhenaclientreadss

pointers - 如果参数的类型是 interface{} 你怎么知道你是按指针传递还是按值传递?

给定任何采用interface{}类型参数的函数,我如何知道是否传递带有或不带有&的参数,而无需浏览源代码函数。例如,如果我有一个具有此类型签名的函数:funcfoo(xinterface{},yint)int是否有任何方法可以确定x应该按值还是按指针传递? 最佳答案 这是来自源代码的片段://DecodeElementworkslikeUnmarshalexceptthatittakes//apointertothestartXMLelementtodecodeintov.//Itisusefulwhenaclientreadss

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 - 无法使用 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{}此错误的原因是什么,我该如何解决?似