草庐IT

Interface1

全部标签

json - 为什么我的代码在我输入 assert 接口(interface)时会出现 panic?

我有一些服务器代码向端点发送请求并接收存储在空接口(interface)类型对象中的JSON响应。我必须解析信息并将其存储在一片“Resource”对象中,其中Resource是一个接口(interface)。在我的例子中,JSON数据表示一个“Position”对象,它满足Resource接口(interface)。所以基本上这些代码看起来像这样://ResourceinterfacetypetypeResourceinterface{//IdentifierreturnstheidfortheobjectIdentifier()bson.ObjectId//Descriptiong

function - 指向函数参数中接口(interface) slice 的指针

我有以下功能:funcread(filePathstring,structure*[]interface){raw,err:=ioutil.ReadFile(filePath)iferr!=nil{fmt.Println(err.Error())os.Exit(1)}json.Unmarshal(raw,structure)}我这样调用它:indexes:=[]Indexread(path+"/"+element+".json",&indexes)但是,当我从函数声明中删除structure*[]interface时,我遇到了奇怪的错误,该错误消失了:./index.verb.go:7

go - 接口(interface)声明差异

作为初学者gopher,我正在努力理解它们之间的区别:m:=map[string]interface{}{}和m:=make(map[string]interface{})我试图找到一个解释,但找不到任何东西。 最佳答案 除了使用make时,两者都是相同的,您可以(当然是可选的)指定您尝试初始化的map的大小。例如,m:=make(map[string]interface{},10)此外,当您像这样初始化map字面量时-m:=map[string]interface{}{}您可以随时为其添加值。在这种情况下,您已将其初始化为nil映

Go type assertion with interface 不明白它是怎么做到的

我正在阅读“GoBootcamp”,第3章第20页中有一个示例我无法理解。在此示例中,在printString(s)行中,s是fakeString类型的变量,但在开关中,进入“Stringer”情况。我试图了解这怎么可能。任何帮助,将不胜感激。代码是:packagemainimport"fmt"typeStringerinterface{String()string}typefakeStringstruct{contentstring}//functionusedtoimplementtheStringerinterfacefunc(s*fakeString)String()strin

go - 不能将接口(interface)的实现用作需要接口(interface)的 func 的参数

我收到以下错误:./main.go:31:cannotusetelegramService(typemessaging.TelegramService)astypemypackage.MessagingServiceinargumenttomypackage.RegisterMessagingService:messaging.TelegramServicedoesnotimplementmypackage.MessagingService(wrongtypeforHandleIncomingMessagemethod)haveHandleIncomingMessage(telegra

go - 将指针传递给接口(interface)时函数抛出错误?

这个问题在这里已经有了答案:"ispointertointerface,notinterface"confusion(2个答案)关闭4年前。所以这就是我遇到的,我不明白为什么会出错:packagemainimport("fmt")//defineabasicinterfacetypeIinterface{get_name()string}//defineastructthatimplementsthe"I"interfacetypeFoostruct{Namestring}func(f*Foo)get_name()string{returnf.Name}//definetwoprint

go - 接口(interface)的结构嵌入, panic : runtime error

我正在尝试一个与接口(interface)的结构嵌入相关的示例//https://talks.golang.org/2014/go4java.slide#52//Structembeddingofinterfaces//https://play.golang.org/p/SYiZ7M1OEhUpackagemainimport("bytes""fmt""net")//net.ConnhasReadandWritetypeloopBackstruct{net.Connbufbytes.Buffer}func(c*loopBack)Read(b[]byte)(int,error){fmt.

go - 如何将接口(interface)映射的映射转换为字符串

我是golang的新手,我有一种如下所示的变量:typeResultDatamap[string]map[string][]interface{}当我收到此变量中的数据时,如何在Go中将整个数据转换为单个字符串? 最佳答案 你可以使用类似Sprintf的东西:funcmain(){d1:=map[string][]interface{}{"a":[]interface{}{20,"hello"},"b":[]interface{}{100}}d2:=map[string][]interface{}{"x":[]interface{}

go - 与具有 interface{} 参数的方法的接口(interface)不起作用

我正在尝试在Go中实现一些接口(interface)。我有接口(interface)A:typeInterfaceAinterface{read(interface{})string}然后我有InterfaceB:typeInterfaceBinterface{fetch()}我有一个函数:funcRead(aInterfaceA){}我有StructA,它通过它的方法满足InterfaceA,但它没有变量“interface{}”,而是像这样传递给InterfaceB:typeStructAstruct{}func(a*StructA)read(bInterfaceB)string{

user-interface - 为什么找不到来自 andlabs/ui 包的组件

我正在尝试按照简单的代码(修改自here)来创建一个窗口:packagemainimport("github.com/andlabs/ui")funcmain(){ui.Main(makeMainWin)}funcmakeMainWin(){varbmiButton=ui.NewButton("First")varotherButton=ui.NewButton("Second")//ui.NewVerticalStackdoesnotwork;stack:=ui.NewVerticalStack(ui.NewLabel("Selectmodule"),bmiButton,otherB