草庐IT

midi-interface

全部标签

pointers - 在 golang 中打印一组空接口(interface)?

我有一个结构,其字段形式为field[]interface{}。如果我打印该字段,我会得到一个指针引用。如果我尝试取消引用该字段,我会收到“无效间接”错误。代码如下:typeMyTypestruct{field[]interface{}}myType:=//createaMyType.Fieldisjustanarrayofnumbersprintln(myType.field)//printsapointerreference,ex:[1/1]0xc420269aa0println(*(myType.field))//doesn'tcompile如何打印myType.field中的值

dictionary - 戈朗 : Using a defined interface in a Map Value

在链接的Playground示例中,我定义了一个类型:typeDoMapmap[int]func(Doer)stringDoer在我定义的接口(interface)类型中。我在具体类型MyDoer上实现接口(interface)。我希望能够构造一个DoMap,其中该映射中的条目包含如下两个函数:func(Doer)string//thisworksfunc(*MyDoer)string//thisdoesn'tfunc(MyDoer)string//more-or-lessthesameidea,alsodoesn't我不能,虽然很明显它们是不同的类型,但我想知道为什么我不能,因为函数

go - 将数据添加到map [string] interface {}

我有以下代码:packagemainimport("sync""fmt")vardataSet=struct{sync.RWMutexdatamap[string]interface{}}{data:make(map[string]interface{})}funcmain(){dataSet.Lock()deferdataSet.Unlock()d:=dataSet.datatest:=[]string{"one","two","three"}m:=map[string]int{"one":1,"two":2,"three":3,}for_,t:=rangetest{d["data"

go - 如何将值附加到 ...interface{}?

这是我要模拟的示例函数:packagemainimport"fmt"funcmain(){fmt.Println(fmt.Sprintf("InitiallyIwant%d%s",2,"dogs"))fmt.Println(Sprintf2("NowIwant%d%s",2,"dogs"))}funcSprintf2(formatstring,a...interface{})string{returnfmt.Sprintf(format+"and%dcats",append(a,5))}这是playground中的一个链接:https://play.golang.org/p/dHDwT

go - "Cannot use myType as type interface{}"?我以为在 Go 中所有类型都算作 interface{}?

这个问题在这里已经有了答案:Convert[]stringto[]interface{}[duplicate](3个答案)Convertingsliceofstructstosliceofemptyinterface[duplicate](1个回答)Whycan'tIsubstituteasliceofonetypeforanotherinGo?(3个答案)Whycan'tIpassa`func()[]int`as`func()[]interface{}`ingo?(2个答案)Whyaslice[]structdoesn'tbehavesameas[]builtin?(3个答案)关闭4

json - 将接口(interface)传递给函数

我正在尝试编写一个采用json文件名和配置结构的配置包。它应该将json解码到传入的结构中并返回它。我正在尝试使用接口(interface),以便我可以传递任何我想要的结构错误是:panic:接口(interface)转换:interface{}是map[string]interface{},不是*main.ConfigurationData我不太确定如何解决这个问题。这是我的主包packagemainimport("config""commons")typeConfigurationDatastruct{S3ARNstring`json:"S3ARN"`SQSQueueUrlstri

go - 创建一个函数来测试从接口(interface)编码/解码

我想创建一个简单的函数来测试编码/解码记录是否按预期工作。我只是在这个例子中使用JSON:packagetestimport("encoding/json""fmt""testing""reflect""github.com/stretchr/testify/require")funcCheckRoundTripJSON(t*testing.T,recordinterface{}){data,err:=json.Marshal(record)require.NoError(t,err)fmt.Println("Record:",record,"wasencodedto:",data,"

go - 如何知道结构或结构指针是否实现接口(interface)

我需要知道结构或指向该结构的指针是否实现了给定的接口(interface)。//Youcaneditthiscode!//Clickhereandstarttyping.packagemainimport"fmt"funcmain(){varaA=A{i:5,}Serialize(a)Serialize(&a)}typeSerializableinterface{//Serialize()string//Deserialize(string)Serializebyte()[]byteDeserializebyte(b[]byte)(bytesReadint)}typeAstruct{i

slice 接口(interface)上的 Golang Switch Case

是否可以区分switchcase中的[]interface{}和interface{}?尝试创建一个解码函数,您可以在其中传递不同的类型,然后switchcase确定类型,然后继续解码该特定类型。虽然当传递的类型是[]interface{}时我遇到了问题。我一直在试验reflect包,但到目前为止运气不好。请参阅下面的代码片段和Playground链接。packagemainimport("fmt""math/big")typeTeststruct{tinterface{}}funcmain(){testVar1:=big.NewInt(0)testVar2:=int64(1)test

go - 为什么要去 json.Unmarshal auto convert interface{} to map

程序会收到很多msg,msg有不同的struct“Data”,所以我定义了Msg结构体:typeMsgstruct{MsgTypeintDatainterface{}}typeData1struct{//msgtype1Datastruct}typeData2struct{//msgtype2Datastruct}func(msgStrstring){msg:=Msg{}iferr:=json.Unmarshal([]byte(msgStr),&msg);err!=nil{//logerr}switchmsg.MsgType{case1://convertmsg.Datatoatype