草庐IT

接口封装

全部标签

go - 关于接口(interface)分配的困惑

这个问题在这里已经有了答案:MethodSets(PointervsValueReceiver)(3个答案)关闭3年前。我对下面的Go代码很困惑。谁能告诉我为什么worker=u和work=&u是否有效?worker=p有效吗?worker=&p无效?User和People有什么区别?packagemainimport("fmt")typeWorkerinterface{Work()}typeUserstruct{namestring}func(uUser)Work(){}typePeoplestruct{namestring}func(p*People)Work(){}funcmai

oop - Golang 在具有私有(private)访问权限的结构中嵌入接口(interface)

我想以尽可能最惯用的方式在Golang中复制以下Java代码:publicclassHandler{privateStoragestorage;privateMappermapper;publicHandler(Storagestorage,Mappermapper){this.storage=storage;this.mapper=mapper;}publicvoidhandleKey(Stringk){storage.put(k,mapper.map(k));}}interfaceStorage{publicvoidput(Stringk,Stringv);publicString

go - 如何遍历接口(interface)的一部分?

Go还是新手。我正在尝试实现答案assuggestedheretomypreviousquestion.在这种情况下,我有一个动物界面和一堆动物结构。我希望能够遍历每只动物并获得它的语言。我已经尝试了一个指针列表,但我不断收到错误消息“y.languageundefined(动物类型没有字段或方法语言)”:Myplaygroundcodepackagemainimport"fmt"typeanimalinterface{speak()}typedogstruct{languagestring}func(d*dog)speak(){d.language="woof"}varn=[]ani

go - Reader接口(interface)改变值

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

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 - 在非本地包中扩展接口(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 中包含接口(interface)时的 new 与 {}

关闭。这个问题是notreproducibleorwascausedbytypos.它目前不接受答案。想改善这个问题吗?更新问题,使其成为on-topic对于堆栈溢出。2年前关闭。Improvethisquestion我有一个问题,就像下面的代码一样。packagemaintypeIinterface{Get()}typeAnimalstruct{}func(a*Animal)Get(){}typeDogstruct{Animal}funcmain(){variIi=new(Dog)//successi=Dog{}//error}游乐场:https://play.golang.org/