草庐IT

multiple-interface-implem

全部标签

go - 如何将结构分配给接口(interface)

这是我的代码:typeICacheEngineinterface{//...}//implementsallmethodsofICacheEnginetypeRedisCachestruct{}typeApplicationCachestruct{Cache*ICacheEngine}funcNewRedisCache()*ApplicationCache{appCache:=new(ApplicationCache)redisCache:=new(RedisCache)appCache.Cache=redisCache//hereisanerror:cannotuse*RedisCa

go - 如何在 Go 中迭代 []interface{}

我正在努力获取以下接口(interface)的键和值,这是对Execute返回的结果进行JSON编码的结果。如thisexample所示:[[{"id":36,"label":"TestThing","properties":{"schema__testBoolean":[{"id":40,"value":true}],"schema__testInt":[{"id":39,"value":1}],"schema__testNumber":[{"id":38,"value":1.0879834}],"schema__testString":[{"id":37,"value":"foob

go - 如何在 Go 中迭代 []interface{}

我正在努力获取以下接口(interface)的键和值,这是对Execute返回的结果进行JSON编码的结果。如thisexample所示:[[{"id":36,"label":"TestThing","properties":{"schema__testBoolean":[{"id":40,"value":true}],"schema__testInt":[{"id":39,"value":1}],"schema__testNumber":[{"id":38,"value":1.0879834}],"schema__testString":[{"id":37,"value":"foob

go - HandlerFunc(f) 如何将函数转换为接口(interface)类型?

检查以下代码时,对从函数到接口(interface)的类型转换有疑问。代码http_hello.go:packagemainimport("fmt""log""net/http")//hellohttp,funchelloHttp(){//registerhandler,http.Handle("/",http.HandlerFunc(helloHandler))//startserver,err:=http.ListenAndServe(":9090",nil)iferr!=nil{log.Fatal("ListenAndServe:",err)}}//handlerfunction

go - HandlerFunc(f) 如何将函数转换为接口(interface)类型?

检查以下代码时,对从函数到接口(interface)的类型转换有疑问。代码http_hello.go:packagemainimport("fmt""log""net/http")//hellohttp,funchelloHttp(){//registerhandler,http.Handle("/",http.HandlerFunc(helloHandler))//startserver,err:=http.ListenAndServe(":9090",nil)iferr!=nil{log.Fatal("ListenAndServe:",err)}}//handlerfunction

go - 在编写可能最终被传递给任何类型的结构的函数时,我可以使用接口(interface)作为参数吗?

我正在尝试编写一个函数,它最终可能采用任何类型的结构......假设它是这样的:funcsetDate(stimestamp,datainterface{}){data.Date=timestamp}我意识到我在现实生活中不需要一个函数来设置这个值,我正在尝试了解更多关于接口(interface)如何工作等的信息。 最佳答案 您可以那样处理它,但随后在setDate()中您需要使用反射来设置Date字段。Go是一种静态类型语言,所以如果data的(静态)类型是interface{}(什么也没说),你真的不能做任何有用的事情用它(你不

go - 在编写可能最终被传递给任何类型的结构的函数时,我可以使用接口(interface)作为参数吗?

我正在尝试编写一个函数,它最终可能采用任何类型的结构......假设它是这样的:funcsetDate(stimestamp,datainterface{}){data.Date=timestamp}我意识到我在现实生活中不需要一个函数来设置这个值,我正在尝试了解更多关于接口(interface)如何工作等的信息。 最佳答案 您可以那样处理它,但随后在setDate()中您需要使用反射来设置Date字段。Go是一种静态类型语言,所以如果data的(静态)类型是interface{}(什么也没说),你真的不能做任何有用的事情用它(你不

pointers - 如何在golang中通过引用传递struct类型的接口(interface)?

我需要通过引用传递结构类型的接口(interface),如下所示。因为,我不能使用接口(interface)指针来构造类型变量,我应该如何更改下面的代码来将te值修改为10?packagemainimport("fmt")funcanother(te*interface{}){*te=check{Val:10}}funcsome(te*interface{}){*te=check{Val:20}another(te)}typecheckstruct{Valint}funcmain(){a:=check{Val:100}p:=&afmt.Println(*p)some(p)fmt.Pri

pointers - 如何在golang中通过引用传递struct类型的接口(interface)?

我需要通过引用传递结构类型的接口(interface),如下所示。因为,我不能使用接口(interface)指针来构造类型变量,我应该如何更改下面的代码来将te值修改为10?packagemainimport("fmt")funcanother(te*interface{}){*te=check{Val:10}}funcsome(te*interface{}){*te=check{Val:20}another(te)}typecheckstruct{Valint}funcmain(){a:=check{Val:100}p:=&afmt.Println(*p)some(p)fmt.Pri

go - 类型断言是处理接口(interface)时返回结构指针的唯一方法吗?

考虑以下代码:packagemainimport"fmt"//5typeIinterface{Foo()string}typeastruct{iint}func(sa)Foo()string{return"Wearehere!"}func(sa)Bar()string{return"MajorTom!"}//20funcmain(){varxI=&a{i:42}fmt.Println(x.Foo())fmt.Println(x.(*a).Bar())}main的最后一条语句返回了底层结构,但我需要导出该结构才能返回。如果我在一个库中使用一个包,其中唯一导出的符号是接口(interfac