草庐IT

INTERFACE

全部标签

go - 在golang中迭代列表json对象

我有这段代码可以读取一个JSON对象。我需要轻松地遍历“outputs”/data/concepts键中的所有元素。有更好的方法吗?此外,我如何访问值的属性:value.app_id,value.id..etc代码:packagemainimport("encoding/json""fmt")varjsonBytes=[]byte(`{"outputs":[{"data":{"concepts":[{"app_id":"main","id":"ai_GTvMbVGh","name":"ancient","value":0.99875855}]}}],"status":{"code":1

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

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

go - 如何将结构转换为基本类型,而不是 Go 中的直接类型?

我需要将未知立即类型的接口(interface)转换为已知的基本类型。例子packagemainimport("fmt""reflect")typeAstruct{foostring}typeBAfuncmain(){bS:=B{foo:"foo"}bI:=reflect.ValueOf(bS).Interface()fmt.Println(bI)aS:=bI.(A)fmt.Println(aS)}当这段代码运行时,可以理解的是,它会出现panic并显示以下消息panic:interfaceconversion:interface{}ismain.B,notmain.A在这个例子中:类

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"},

json - 读取 map[string]interface{} 中的数据

源服务器返回多个对象的Json格式的数据接口(interface),我们如何解析这些数据?我正在使用JSONmap[string]interface{}类型的变量来保存来自服务器的结果ThedatareturnfromServer."data":[{"group":"PAA_TEST","id":"2018-04-10T09:24:18.000000Z","name":"PAA_STATION","released":true,"version":33},{"group":"PAA_TEST","id":"2018-03-19T10:50:21.000000Z","name":"PAA

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 - interface{}转unsafe.Pointer问题

我正在做一些关于unsafe.Pointer的测试。假设TestFun是一个普通的库函数,那么“Person”结构不应该出现在这个函数中typePersonstruct{Namestring}funcmain(){p:=&Person{}p.Name="TestName"TestFun(p)}funcTestFun(valinterface{}){//Iwanttoconvertinterface{}tounsafe.Pointervarpointerunsafe.Pointer//ThisLinecompileerror//pointer=unsafe.Pointer(val)//T

go - 如何在另一个 map 界面中添加一个 map 界面?

这里我有一个map界面的消息。其中有2个键key1,key2。Key2有2个键k2、k3。我想在其中添加另一个键。所以我这样做了。message:=map[string]interface{}{"key1":map[string]string{"k1":"",},"key2":map[string]interface{}{"k2":"","k3":map[string]interface{}{"kk1":"",},},}k:=map[string]interface{}{"kk2":"",}message["key2"]["k4"]=kb,err:=json.Marshal(messa