草庐IT

Interface1

全部标签

arrays - 不能在赋值 : need type assertion 中使用字(类型接口(interface) {})作为类型字符串

我是Go的新手,出于某种原因我正在做的事情对我来说似乎不是很直接。这是我的代码:for_,column:=rangeresp.Values{for_,word:=rangecolumn{s:=make([]string,1)s[0]=wordfmt.Print(s,"\n")}}我得到了错误:不能在赋值中使用word(typeinterface{})作为类型字符串:需要类型断言resp.Values是一个数组数组,所有数组都填充有字符串。reflect.TypeOf(resp.Values)返回[][]interface{},reflect.TypeOf(resp.Values[0])

arrays - 使用解码到通用接口(interface)时如何验证 JSON?

我想验证字节数组数据是否包含有效的JSON,使用unmarsall方法进入接口(interface)。packagemainimport("encoding/json""fmt")funcisJSON(sstring)bool{varjsmap[string]interface{}returnjson.Unmarshal([]byte(s),&js)==nil}funcmain(){vartests=[]string{`{"a":"b"}`,`[{"a":"b"},{"a":"b"}]`,}for_,t:=rangetests{fmt.Printf("isJSON(%s)=%v\n\n

go - 从函数返回空接口(interface)的 slice

有人可以解释为什么这不起作用吗?我们如何从示例中所示的函数返回接口(interface)片段[]interface{}?import("fmt")funcmain(){vartest[]stringTest(&test)fmt.Println(test)}funcTest(tinterface{}){a:=[]interface{}{"first","second"}fmt.Println(a)t=a}可在此处找到运行代码的示例:https://play.golang.org/p/vcEGHSdWrjv顺便说一句,这是我试图从中提取数据的func:https://godoc.org/g

go - 将通用结构/接口(interface)传递给函数并返回它

我可以将通用结构或接口(interface)传递给函数,然后返回它吗?我试过在下面的例子中使用指针,我也试过使用struct作为返回类型,但我似乎做不到。如果我改为使用interface{},我似乎能够传入postData,但通过返回或更新指针来取回它似乎是不可能的。谁能告诉我哪里出错了?funcEmailHandler(writerhttp.ResponseWriter,request*http.Request){varpostData=EmailPostData{}ConvertRequestJsonToJson(request,&postData)}funcConvertRequ

go - 如何将接口(interface)传递给有很多参数的方法

我写了一个惰性代码来演示我必须实现接口(interface)的问题。我有方法M1、M2,它们将结构X作为参数并具有结构Y的类型。我希望所有这些方法都由单个接口(interface)I实现。问题是实现接口(interface)的方法M我需要注意需要传递给子方法(M1,M2)的参数。我得到一个错误:usedasavalue当我在M中传递多个参数时typeYstruct{aint}typeXstruct{aint}(y*Y)funcM1(xX)struct{returny.a+x.a}(y*Y)funcM2(xX)struct{returny.a*x.a}typeIinterface{M1(

go - 被嵌入在golang结构中的接口(interface)所困惑

像这里的代码,当把一个接口(interface)A嵌入到结构体B中,然后将A设置为aa,aa是AA的一个实例。B和AA都有元素X,当调用b.X时我刚得到B.X。我怎样才能得到b.AA.X?我知道这个语法是有线的,但我只是想弄清楚varb是如何存储在内存中的,我尝试了一些不安全的语法,没办法得到b.A.X.:packagemainimport("fmt""unsafe")typeAinterface{Hello()string}typeBstruct{AXstring}typeAAstruct{numintXstring}func(aaAA)Hello()string{returnfmt

go - 在 golang 中声明一个空的 map[string]interface{} 的内存成本/开销是多少?

这个问题在这里已经有了答案:MemoryoverheadofmapsinGo(5个答案)关闭3年前。出于好奇,来自sourcecodetypehmapstruct{countint//1wordflagsuint8Buint8noverflowuint16hash0uint32//=8bytebucketsunsafe.Pointer//1wordoldbucketsunsafe.Pointer//1wordnevacuateuintptr//1wordextra*mapextra//1word}所以它至少是:5字+8字节但为什么creationcostis0?-packagemain

go - dynamodbattribute.UnmarshalMap 将我的变量类型更改为 map[string]interface{}

背景我正在尝试将dynamodb.GetItem返回的项目解码到一个对象中,我在那个地方不知道是哪种类型。为此,我有一个函数emptyItemConstructor,它返回所需类型的新对象t。问题我有这样一个函数:funcGetItem(emptyItemConstructorfunc()interface{})interface{}{myItem:=emptyItemConstructor()fmt.Printf("Typeis:%T\n",myItem)_=dynamodbattribute.UnmarshalMap(item,&myItem)fmt.Printf("Typenow

interface - 类型上的 Golang 接口(interface)

我是GO的新手,我正在使用golang编写一个简单的类型接口(interface)。类型定义为:typeSequence[]float64andtheinterfaceis:typeStatsinterface{greaterThan(xfloat64)Sequence}函数greaterThan(xfloat64)应该返回一个与对象中的数字相同的新序列//除了所有小于或等于x的数字都已被删除。这是我的尝试,但无法编译。我不知道如何解决它。我的问题是:如何从结构类型中删除项目?我应该使用map吗?(作为我的尝试)packagemainimport"fmt"typeSequence[]f

interface - Go:工厂返回指针和接口(interface){}类型

考虑以下工厂:GoplaygroundtypeTypeAstruct{placeholderint}funcNewTypeA()*TypeA{returnnew(TypeA)}funcmain(){factory:=make(map[string]func()interface{})factory["TypeA"]=NewTypeA}这给了我以下错误:cannotuseNewTypeA(typefunc()*TypeA)astypefunc()interface{}inassignment这很清楚。我认为interface{}可以与指针匹配。我找到了这个解决方案:Goplaygroun