jjzjj

interface-builder

全部标签

syntax - 戈朗 : how to get sub element from a []interface{} alias

我为[]interface{}定义了一个别名:typestate[]interface{}如何获取状态中的子项:functest(sstate){//Howtoget1stelementins?//orHowtoconvertsbackto[]interface{}?}test([]interface{1,2,3}) 最佳答案 test([]interface{1,2,3})是错误的,应该是test(state{1,2,3}).您还可以像访问任何slice一样访问s中的第一个元素,使用s[x]:typestate[]interfac

struct - 在将它作为接口(interface)传递并运行它的方法后,让你将结构恢复为它的数据类型?

我有一个结构如下typeMyStruct{EmbeddedFooBar}func(m*MyStruct)Foo(b*http.Request){//Doingsomething}funcfn(args...interfaces){//It'shereIwanttogetmystructbackandrunthe"Get"method//PleasekeepinmindIamtoopassapointerparamintothestructmethodstrt:=args[0]....getstructbacktostaticdatatypeMyStructandrun"Get()",d

php - 高语 : create a function that accept an interface (I came from PHP)

在PHP中我可以创建一个接口(interface)interfaceHello{publicfunctionbar();}以及一些实现它的类finalclassFooimplementsHello{publicfunctionbar(){//dosomething}}finalclassBarimplementsHello{publicfunctionbar(){//dosomething}}然后,我还可以创建一个接受该接口(interface)的NewClass::bar()方法。finalclassNewClass{publicfunctionbar(Hello$hello){//

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