草庐IT

Interface1

全部标签

go - Reader接口(interface)改变值

我有一个关于阅读器界面的问题,定义如下:typeReaderinterface{Read(p[]byte)(nint,errerror)}我有以下使用阅读器界面的代码:packagemainimport("fmt""os")//Readingfilesrequirescheckingmostcallsforerrors.//Thishelperwillstreamlineourerrorchecksbelow.funccheck(eerror){ife!=nil{panic(e)}}funcmain(){//You'lloftenwantmorecontroloverhowandwha

algorithm - golang []interface{} 不能作为函数参数吗?

这个问题在这里已经有了答案:Typeconvertingslicesofinterfaces(9个回答)关闭7年前。我的代码:packagesort_testtypeSortList[]interface{}typeSortFuncfunc(interface{},interface{})boolfuncDo(listSortList,functionSortFunc)主包packagemainimport("sort_test")funcmain(){list:=[]int{3,4,5,6,6,77,4,4,5,6,8,345,45,424,2,67,7,830}slice:=lis

go - 将 []interface{} 转换为非可变函数的参数

我正在寻找一种优雅的方式来解压缩Go中的参数列表。我不想为此目的使用可变参数函数,因为在我编写函数的用例中,我已经知道参数的数量,我想保持这部分简单。但是在我的用例中,参数作为[]interface{}到达。我找不到解决方案,但也许有人已经知道该怎么做?packagemainimport("fmt")//NON-VARIADICgreaterfuncgreet(n1,n2string){fmt.Printf("%s%s\n",n1,n2)}funcmain(){l:=[]interface{}{"hello","world"}//worksgreet(l[0].(string),l[1

go - 在父类(super class)的子类上找不到接口(interface)方法

这个问题在这里已经有了答案:isitpossibletocalloverriddenmethodfromparentstructinGolang?(6个答案)关闭6年前。鉴于此代码...typeBaseItf1interface{getName()stringclone()*BaseStruct}typeBaseStructstruct{BaseItf1}func(bs*BaseStruct)cloneAndGetName()string{sc:=bs.clone()returnsc.getName()}typeSubClassstruct{BaseStruct}func(sc*Sub

go - 一个接口(interface),多种实现

如何将以下Java代码翻译成Go?interfaceNamePrinter{voidprint();}classNamePrinterWithoutGreetingimplementsNamePrinter{privatestringname;publicNamePrinterWithoutGreeting(stringname){this.name=name;}publicvoidprint(){System.out.println(this.name);}}classNamePrinterWithGreetingimplementsNamePrinter{privatestring

go - 如何将不同的值从 map[string]interface{} 转换为类型字符串

我在interface{}中有一个具有不同类型的映射,我需要将它们全部转换为字符串类型。类型断言是不够的。packagemainfuncmain(){map1:=map[string]interface{}{"str1":"stringone","int1":123,"float1":0.123}varslc[]stringfor_,j:=rangemap1{slc=append(slc,j.(string))//panic:interfaceconversion:interface{}isint,notstring}} 最佳答案

go - 在非本地包中扩展接口(interface)方法

尝试在Go中创建微服务,我有一个包网络来处理获取字节并转换为特定请求:packagenetworktypeRequestinterface{}typeRequestAstruct{aint}typeRequestBstruct{bstring}funcGetRequestFromBytes(connnet.Conn)Request{buf:=make([]byte,100)_,_:=conn.Read(buf)switchbuf[0]{case0://convertbytesintoRequestArequestA=RequestAFromBytes(buf[1:])returnreq

go - 如何将接口(interface) {} 转换为结构

我一直在寻找如何将接口(interface)转换为结构,但我不知道如何做不到。我会尽力解释我的问题。typeResultstruct{Http_codeintHttp_msgstringResponseinterface{}}此结构由向服务器发出HTTP请求的函数返回,另一方面,我有不同类型的结构来包装响应。这是我要转换接口(interface)的结构。typeResHealthstruct{TypestringGet_healthstruct{Healthybool}}我的问题是,当我尝试做出断言时,我总是遇到段冲突或程序无法编译。工作流程是:packagetesttypeResul

go - 接口(interface)[golang]中的字段?

是否有强制struct具有特定属性(在接口(interface)中定义)的解决方案?或者在接口(interface)中定义属性(属性,字段)?如我所见,接口(interface)总是接受方法而不是属性?。(https://gobyexample.com/interfaces)typegeointerface{PrintType()typstring//notfunction,butfield}typecirclestruct{typstring}func(ccircle)PrintType(){fmt.Println(c.typ)}谢谢 最佳答案

go - 为什么我不能在 go 中将 `func() []int` 作为 `func() []interface{}` 传递?

我有以下定义:func(c*Collector)RegisterSource(ffunc()[]interface{}){c.source=f}我尝试按如下方式调用它但出现错误:funcsource()[]int{return[]int{0,1,2,3,4}}...c.RegisterSource(source)这会遇到:cannotusesource(typefunc()[]int)astypefunc()[]interface{}inargumenttoc.RegisterSource 最佳答案 relevantGoFAQent