草庐IT

接口封装

全部标签

go - slice of slice 中的接口(interface)转换

我写了这个示例代码(https://play.golang.org/p/u_oz5X4aU07):funcmain(){varobjinterface{}json.Unmarshal([]byte("[[1,2],[3,4]]"),&obj)val:=obj.([][]int)fmt.Println(val)}为什么会出现错误:interfaceconversion:interface{}is[]interface{},not[][]int有没有一种简单的方法可以将obj转换成一片slice?此代码有效,但我想要更紧凑和高效的代码。varval[][]float64forr,v:=ra

json - 在 golang 中存储和检索接口(interface)

我们如何将不同结构的数组存储到某个文件中并以相同的格式检索它而不丢失其属性(它提供的方法)。例如:我有数据structA和structB,它们都使用一些方法实现了一个通用的interfaceX{}。一个选项是编写保存和检索方法来接受接口(interface)Xslice。但是问题是如何以某种与我的数据结构无关的通用方式将其解码。即,每次我添加一个新的数据结构时,我不需要更改我的保存或检索函数来检索接口(interface)X的slice,以便它的方法可以独立于数据结构使用。解码抛出错误的示例:GoPlayGroundLinkwithasmallExample

pointers - 将指针 slice 分配给它们实现的接口(interface) slice

这个问题在这里已经有了答案:Typeconvertingslicesofinterfaces(9个回答)关闭8个月前。我需要了解golang的行为。想象一下,我们有一个带有某种方法的接口(interface),并且我们有一个实现该方法的类型。如果我们将指向类型的指针分配给定义为接口(interface)的变量,golang允许我们这样做。但是当我们尝试将指针slice分配给定义为包含接口(interface)slice的变量时,golang会出现panic...谁能解释一下为什么?Hereisanexample

go - 对结构字段进行持续更改*并*满足 Writer 接口(interface)?

这个问题在这里已经有了答案:XdoesnotimplementY(...methodhasapointerreceiver)(4个答案)关闭4年前。为了实际更改方法中的struct字段,您需要一个指针类型的接收器。Iunderstandthat.为什么我不能用指针接收器满足io.Writer接口(interface),以便我可以更改结构字段?有没有惯用的方法来做到这一点?//CountWriterisatyperepresentingawriterthatalsocountstypeCountWriterstruct{CountintOutputio.Writer}func(cw*Co

go - 接口(interface)指针的奇怪行为

我写了3个类似的函数来找出Go指针反射的一个奇怪行为。packagemainimport("reflect""fmt")variinterface{}=struct{}{}//iisaninterfacewhichpointstoastructvarptr*interface{}=&i//ptrisi'spointerfuncf(xinterface{}){//printx'sunderlyingvaluefmt.Println(reflect.ValueOf(x).Elem())}funcmain1(){//fisaskingforinterface?OK,I'llusethestr

go - 在 Golang 中将接口(interface)类型实现为函数类型

我创建了几种类型,包括接口(interface)://GetProfileHandlerFuncturnsafunctionwiththerightsignatureintoagetprofilehandlertypeGetProfileHandlerFuncfunc(GetProfileParams,interface{})middleware.Responder//Handleexecutingtherequestandreturningaresponsefunc(fnGetProfileHandlerFunc)Handle(paramsGetProfileParams,princ

go - context 在使用 Value(key) 后返回 nil 接口(interface)

我有一个ctx(context.Context)变量,它的值为:ctx=context.Background.WithCancel.WithCancel.WithValue(peer.peerKey{},&peer.Peer{Addr:(*net.UnixAddr)(0xc000270820),AuthInfo:credentials.AuthInfo(nil)}).WithValue(metadata.mdIncomingKey{},metadata.MD{":authority":[]string{"unix:///run/containerd/containerd.sock"},

go - append 到实现的基本接口(interface) slice 的 slice

为什么以下不起作用?locations:=make([]*LocationEvent,0)data:=make([]Event,0)data=append(data,locations...)其中*LocationEvent(结构)实现了Event(接口(interface))。虽然以下工作正常:data=append(data,&LocationEvent{},&LocationEvent{})那么当使用...扩展实际的[]*LocationEventslice时有何不同? 最佳答案 slice类型必须与append函数中的可变参

go - 在 go wiki 中解释 go 接口(interface)定义

我了解了一点,并且在一定程度上也了解了界面(比如我如何在ruby​​中进行ducktyping)但是阅读接口(interface)定义https://github.com/golang/go/wiki/CodeReviewComments我不知道要传达什么。第一:我没看懂评论。Gointerfacesgenerallybelonginthepackagethatusesvaluesoftheinterfacetype,notthepackagethatimplementsthosevalues.第二:我不明白这个Donotdefineinterfacesontheimplementor

go - 在 Go 中的接口(interface){}片段上搜索接口(interface){}的函数

我正在尝试实现一个函数,该函数接受任何类型的元素和相同类型的slice,并在第二个中搜索第一个,将其位置作为结果,否则为-1。我不是Go专家,所以我的第一个想法是将要搜索的元素作为interface{}并将slice作为[]interface{}传递,但它并没有真正奏效。这是我尝试过的:packagemainimport("fmt")funcIsElementInListWithPos(elementinterface{},list[]interface{})int{fori:=rangelist{iflist[i]==element{returni}}return-1}funcmai