草庐IT

interface-design

全部标签

go - 使用 Gtk+ 的 C 接口(interface)构建 Go

我正在尝试构建一个使用外部C代码作为Gtk+接口(interface)的Go程序。这是我得到的基本Go代码(ui.h.go):packagemain//#cgopkg-config:gtk+-3.0//#include"ui.h"import"C"funcCInit(){C.Init(nil,0)}funcCMain(){C.Main()}funcCShowWindow(){C.ShowWindow()}funcmain(){CInit()CShowWindow()CMain()}C代码从vala编译成目标文件(ui.o)和头文件(ui.h):#ifndef__UI_H__#defin

go - 使用 Gtk+ 的 C 接口(interface)构建 Go

我正在尝试构建一个使用外部C代码作为Gtk+接口(interface)的Go程序。这是我得到的基本Go代码(ui.h.go):packagemain//#cgopkg-config:gtk+-3.0//#include"ui.h"import"C"funcCInit(){C.Init(nil,0)}funcCMain(){C.Main()}funcCShowWindow(){C.ShowWindow()}funcmain(){CInit()CShowWindow()CMain()}C代码从vala编译成目标文件(ui.o)和头文件(ui.h):#ifndef__UI_H__#defin

testing - 当一个具体方法的签名引用另一个具体类型而不是它的接口(interface)时,我如何模拟多个类型?

我正在使用没有任何类接口(interface)的第三方库。我可以在我的结构中使用它们没问题,但它们有副作用,我想在单元测试时避免。//Somewherethereareacouplestructs,withnointerfaces.Idon'townthecode.//Eachhasonlyonemethod.typeThirdPartyEntrystruct{}func(eThirdPartyEntry)Resolve()string{//Dosomecomplexstuffwithsideeffectsreturn"I'mme!"}//Thisstructreturnsaninst

testing - 当一个具体方法的签名引用另一个具体类型而不是它的接口(interface)时,我如何模拟多个类型?

我正在使用没有任何类接口(interface)的第三方库。我可以在我的结构中使用它们没问题,但它们有副作用,我想在单元测试时避免。//Somewherethereareacouplestructs,withnointerfaces.Idon'townthecode.//Eachhasonlyonemethod.typeThirdPartyEntrystruct{}func(eThirdPartyEntry)Resolve()string{//Dosomecomplexstuffwithsideeffectsreturn"I'mme!"}//Thisstructreturnsaninst

http - Golang 嵌入了一个带有额外隐藏方法的接口(interface)?

我想在结构中嵌入一个http.ResponseWriter。没关系,它确保我的结构也实现了http.ResponseWriter:typeMyWriterstruct{BytesWritteninthttp.ResponseWriter}但是,它不再实现http.Hijacker、http.CloseNotifier或http.Flusher,即使嵌入了http。ResponseWriter通常是这样。有什么办法可以做到这一点吗? 最佳答案 如果您知道响应作者满足问题中列出的所有接口(interface),那么您可以这样做:type

http - Golang 嵌入了一个带有额外隐藏方法的接口(interface)?

我想在结构中嵌入一个http.ResponseWriter。没关系,它确保我的结构也实现了http.ResponseWriter:typeMyWriterstruct{BytesWritteninthttp.ResponseWriter}但是,它不再实现http.Hijacker、http.CloseNotifier或http.Flusher,即使嵌入了http。ResponseWriter通常是这样。有什么办法可以做到这一点吗? 最佳答案 如果您知道响应作者满足问题中列出的所有接口(interface),那么您可以这样做:type

reflection - 通过接口(interface)访问嵌入式类型字段

似乎我错过了一些重要的东西,但我无法弄清楚它是什么。我使用reflect通过接口(interface)访问嵌入式类型字段。我遇到的问题是,根据runtime/pprof,它会占用大量CPU。我不喜欢在所有Vehicles上实现Setter和Getter方法,那么有更好的方法吗?简化示例:packagemainimport("reflect""fmt")//the"contract"isthatallvehicleshaveanembeddedEnginetypeVehicleinterface{}typeEnginestruct{Powerfloat64Cubicfloat64}typ

reflection - 通过接口(interface)访问嵌入式类型字段

似乎我错过了一些重要的东西,但我无法弄清楚它是什么。我使用reflect通过接口(interface)访问嵌入式类型字段。我遇到的问题是,根据runtime/pprof,它会占用大量CPU。我不喜欢在所有Vehicles上实现Setter和Getter方法,那么有更好的方法吗?简化示例:packagemainimport("reflect""fmt")//the"contract"isthatallvehicleshaveanembeddedEnginetypeVehicleinterface{}typeEnginestruct{Powerfloat64Cubicfloat64}typ

oop - 确保嵌入式结构在不引入歧义的情况下实现接口(interface)

我正在尝试通过更好地定义接口(interface)和使用嵌入式结构来重用功能来清理我的代码库。在我的例子中,我有许多可以链接到各种对象的实体类型。我想定义捕获需求的接口(interface)和实现接口(interface)的结构,然后可以将其嵌入到实体中。//AllentitiesimplementthisinterfacetypeEntityinterface{Identifier()Type()}//InterfaceforentitiesthatcanlinkFoostypeFooLinkerinterface{LinkFoo()}typeFooLinkerEntitystruc

oop - 确保嵌入式结构在不引入歧义的情况下实现接口(interface)

我正在尝试通过更好地定义接口(interface)和使用嵌入式结构来重用功能来清理我的代码库。在我的例子中,我有许多可以链接到各种对象的实体类型。我想定义捕获需求的接口(interface)和实现接口(interface)的结构,然后可以将其嵌入到实体中。//AllentitiesimplementthisinterfacetypeEntityinterface{Identifier()Type()}//InterfaceforentitiesthatcanlinkFoostypeFooLinkerinterface{LinkFoo()}typeFooLinkerEntitystruc