草庐IT

multiple-interface-implem

全部标签

go - Golang中多接口(interface)转换的简洁方式?

我正在尝试使用go/ast包从代码中解析函数调用。为此,我首先找到所有函数调用,例如:ast.Inspect(f,func(nast.Node)bool{switchx:=n.(type){case*ast.FuncDecl:processFunction(x)}returntrue})然后,processFunction()看起来像这样:funcprocessFunction(e*ast.FuncDecl){//Savewrapperfunctionnamef:=e.Name.Namefor_,expression:=rangee.Body.List{logrus.Printf("C

go - Golang中多接口(interface)转换的简洁方式?

我正在尝试使用go/ast包从代码中解析函数调用。为此,我首先找到所有函数调用,例如:ast.Inspect(f,func(nast.Node)bool{switchx:=n.(type){case*ast.FuncDecl:processFunction(x)}returntrue})然后,processFunction()看起来像这样:funcprocessFunction(e*ast.FuncDecl){//Savewrapperfunctionnamef:=e.Name.Namefor_,expression:=rangee.Body.List{logrus.Printf("C

go - 使用接口(interface)模拟数据库返回类型

如何使用接口(interface)正确模拟一些*sql.DB方法进行单元测试?例如,我想要一个Store结构用作我的处理程序的数据源。它有一个conn字段,即*sql.DB。但在我的测试中,我想模拟数据库,所以我尝试返回的不是实际的*sql.Rows,而是*sql.Rows应该满足的接口(interface)。这是代码:typeTestRowsinterface{Scan()}typeTestDBinterface{Query(querystring,args...interface{})(TestRows,error)}typeRowsstruct{//..}func(r*Rows)

go - 使用接口(interface)模拟数据库返回类型

如何使用接口(interface)正确模拟一些*sql.DB方法进行单元测试?例如,我想要一个Store结构用作我的处理程序的数据源。它有一个conn字段,即*sql.DB。但在我的测试中,我想模拟数据库,所以我尝试返回的不是实际的*sql.Rows,而是*sql.Rows应该满足的接口(interface)。这是代码:typeTestRowsinterface{Scan()}typeTestDBinterface{Query(querystring,args...interface{})(TestRows,error)}typeRowsstruct{//..}func(r*Rows)

go - interface{} 变量到 []interface{}

我有一个interface{}变量,我知道它是一个指向slice的指针:funcisPointerToSlice(valinterface{})bool{value:=reflect.ValueOf(val)returnvalue.Kind()==reflect.Ptr&&value.Elem().Kind()==reflect.Slice}但我发现很难将其类型转换为[]interface{}变量:ifisPointerToSlice(val){slice,worked:=reflect.ValueOf(val).Elem().Interface().([]interface{})//

go - interface{} 变量到 []interface{}

我有一个interface{}变量,我知道它是一个指向slice的指针:funcisPointerToSlice(valinterface{})bool{value:=reflect.ValueOf(val)returnvalue.Kind()==reflect.Ptr&&value.Elem().Kind()==reflect.Slice}但我发现很难将其类型转换为[]interface{}变量:ifisPointerToSlice(val){slice,worked:=reflect.ValueOf(val).Elem().Interface().([]interface{})//

pointers - 如何获取指向被屏蔽为接口(interface)的变量的指针?

我不想深入探讨以下情况的基本原理。它与解码序列化对象有关,该对象可以是一组固定类型中的任何一种,但您不知道是哪种类型。我有以下类型:typeIinterface{Do()}typesomeIstruct{}func(i*someI)Do(){}typeotherIstruct{}func(i*otherI)Do(){}因此,两个结构其中的指针实现了接口(interface)I。现在我有这个方法想要返回一个I类型的值:funcGetSomeI(marshalled[]byte)(I,error){varobjinterface{}//Thefollowingmethodmagically

pointers - 如何获取指向被屏蔽为接口(interface)的变量的指针?

我不想深入探讨以下情况的基本原理。它与解码序列化对象有关,该对象可以是一组固定类型中的任何一种,但您不知道是哪种类型。我有以下类型:typeIinterface{Do()}typesomeIstruct{}func(i*someI)Do(){}typeotherIstruct{}func(i*otherI)Do(){}因此,两个结构其中的指针实现了接口(interface)I。现在我有这个方法想要返回一个I类型的值:funcGetSomeI(marshalled[]byte)(I,error){varobjinterface{}//Thefollowingmethodmagically

go - 返回它自身的接口(interface)(可克隆)

我正在尝试创建一个可克隆的接口(interface),但在获取结构来实现该接口(interface)时遇到了一些问题。看来这是一个限制,在许多其他语言中都没有。我试图了解此限制的理由。var_Cloneable=test{}typeCloneableinterface{Clone()Cloneable}typeteststruct{}func(t*test)Clone()*test{c:=*treturn&c}Playground:https://play.golang.org/p/Kugatx3Zpw后续问题,因为它对我来说仍然很奇怪。这也不编译var_Cloneable=&test

go - 返回它自身的接口(interface)(可克隆)

我正在尝试创建一个可克隆的接口(interface),但在获取结构来实现该接口(interface)时遇到了一些问题。看来这是一个限制,在许多其他语言中都没有。我试图了解此限制的理由。var_Cloneable=test{}typeCloneableinterface{Clone()Cloneable}typeteststruct{}func(t*test)Clone()*test{c:=*treturn&c}Playground:https://play.golang.org/p/Kugatx3Zpw后续问题,因为它对我来说仍然很奇怪。这也不编译var_Cloneable=&test