草庐IT

multiple-interface-implem

全部标签

go - 如何制作一个只允许整数的接口(interface)?

我想制作一个可以接受任何类似int的变量的函数。例如:typeIntegerTypeinterface{int}funcMyFunc(argIntegerType){fmt.Println(arg)}这不会编译,因为我不能将接口(interface)继承基于非接口(interface)类型,但这正是我想要做的!最终目标是这样的:typeTokenintvartTokent=3MyFunc(t)我如何保证我的参数是一个整数,同时仍然允许整数别名? 最佳答案 HowdoIguaranteethatmyargumentisanintege

go - 如何制作一个只允许整数的接口(interface)?

我想制作一个可以接受任何类似int的变量的函数。例如:typeIntegerTypeinterface{int}funcMyFunc(argIntegerType){fmt.Println(arg)}这不会编译,因为我不能将接口(interface)继承基于非接口(interface)类型,但这正是我想要做的!最终目标是这样的:typeTokenintvartTokent=3MyFunc(t)我如何保证我的参数是一个整数,同时仍然允许整数别名? 最佳答案 HowdoIguaranteethatmyargumentisanintege

go - 用作接口(interface)的复合文字的地址

复合文字的地址在用作接口(interface)时被评估为文字本身。有人可以指出refspec的部分吗?哪个处理这个?packagemainimport"fmt"typentfcinterface{rx()int}typecncrtstruct{xint}func(ccncrt)rx()int{returnc.x}funcrtrnsNtfca()ntfc{return&cncrt{3}}funcrtrnsNtfc()ntfc{returncncrt{3}}funcrtrnsCncrt()*cncrt{return&cncrt{3}}funcmain(){fmt.Println(rtrns

go - 用作接口(interface)的复合文字的地址

复合文字的地址在用作接口(interface)时被评估为文字本身。有人可以指出refspec的部分吗?哪个处理这个?packagemainimport"fmt"typentfcinterface{rx()int}typecncrtstruct{xint}func(ccncrt)rx()int{returnc.x}funcrtrnsNtfca()ntfc{return&cncrt{3}}funcrtrnsNtfc()ntfc{returncncrt{3}}funcrtrnsCncrt()*cncrt{return&cncrt{3}}funcmain(){fmt.Println(rtrns

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