草庐IT

human-interface-guidelines

全部标签

go - 反射值接口(interface)和指针接收器

在golang的mongodb驱动中有如下一段代码:casereflect.Struct:ifz,ok:=v.Interface().(Zeroer);ok{returnz.IsZero()}returnfalseInterfaceZeroer定义如下:typeZeroerinterface{IsZero()bool}当我用实现我的结构时func(idSomeStruct)IsZero()bool{returnid.ID==""}它有效。但是当我使用指针接收器实现IsZero方法时:func(id*SomeStruct)IsZero()bool{returnid.ID==""}类型断言

go - 反射值接口(interface)和指针接收器

在golang的mongodb驱动中有如下一段代码:casereflect.Struct:ifz,ok:=v.Interface().(Zeroer);ok{returnz.IsZero()}returnfalseInterfaceZeroer定义如下:typeZeroerinterface{IsZero()bool}当我用实现我的结构时func(idSomeStruct)IsZero()bool{returnid.ID==""}它有效。但是当我使用指针接收器实现IsZero方法时:func(id*SomeStruct)IsZero()bool{returnid.ID==""}类型断言

concurrency - 为什么我不能在函数参数中使用 type []chan *Message as type []chan interface{}?

这是我收到的错误消息:cannotusec.ReceiverChans(type[]chan*Message)astype[]chaninterface{}infunctionargument 最佳答案 类型不同。*Message实现空接口(interface),但这并不意味着您可以获取*Message的slice或chan并将其传递给需要slice或chan的对象接口(interface)。我将接口(interface)视为特定数据结构的方式;一对指向值的指针和指向基础类型的指针。这不完全是接口(interface)的工作方式,但

concurrency - 为什么我不能在函数参数中使用 type []chan *Message as type []chan interface{}?

这是我收到的错误消息:cannotusec.ReceiverChans(type[]chan*Message)astype[]chaninterface{}infunctionargument 最佳答案 类型不同。*Message实现空接口(interface),但这并不意味着您可以获取*Message的slice或chan并将其传递给需要slice或chan的对象接口(interface)。我将接口(interface)视为特定数据结构的方式;一对指向值的指针和指向基础类型的指针。这不完全是接口(interface)的工作方式,但

Go接口(interface)方法使用

我使用Martini库编写了一个非常简单的Go应用程序。在组合一个简单的RESTAPI示例时,我想在发生如下错误时将JSON数据呈现给用户:{error:"DocumentNotFound",code:404}我正在使用以下代码返回一个map,Martini可以将其呈现为JSON。包错误响应import"net/http"typeErrJsoninterface{RenderErr(vint)}funcRenderErr(vint)map[string]interface{}{varreturnMap=map[string]interface{}{"error":http.Status

Go接口(interface)方法使用

我使用Martini库编写了一个非常简单的Go应用程序。在组合一个简单的RESTAPI示例时,我想在发生如下错误时将JSON数据呈现给用户:{error:"DocumentNotFound",code:404}我正在使用以下代码返回一个map,Martini可以将其呈现为JSON。包错误响应import"net/http"typeErrJsoninterface{RenderErr(vint)}funcRenderErr(vint)map[string]interface{}{varreturnMap=map[string]interface{}{"error":http.Status

go - 如何传递转换为接口(interface){}的函数

我正在尝试编写一个函数来打开一个文件,使用它接收的另一个函数作为参数从每一行构建一个对象并返回这些对象的一部分,类似于此:funcreadFile(filenamestring,transformfunc(string)interface{})(list[]interface{}){ifrawBytes,err:=ioutil.ReadFile(filename);err!=nil{log.Fatal(err)}else{lines:=strings.Split(string(rawBytes),"\n")fori:=rangelines{t:=transform(lines[i])l

go - 如何传递转换为接口(interface){}的函数

我正在尝试编写一个函数来打开一个文件,使用它接收的另一个函数作为参数从每一行构建一个对象并返回这些对象的一部分,类似于此:funcreadFile(filenamestring,transformfunc(string)interface{})(list[]interface{}){ifrawBytes,err:=ioutil.ReadFile(filename);err!=nil{log.Fatal(err)}else{lines:=strings.Split(string(rawBytes),"\n")fori:=rangelines{t:=transform(lines[i])l

go - 如何将 *ast.StructType 断言到指定接口(interface)

我可以断言由*ast.TypeSpec和*ast.StructType表示的结构来实现已知的接口(interface)类型吗?例如funcassertFoo(spec*ast.TypeSpec)bool{//spec.Name=="MyStruct"st,_:=spec.Type.(*ast.StructType)//Iwanttoknowwhether"MyStruct"implements"FooInterface"ornot_,ok:=st.Interface().(FooInterface)returnok}但是没有*ast.StructType.Interface():(

go - 如何将 *ast.StructType 断言到指定接口(interface)

我可以断言由*ast.TypeSpec和*ast.StructType表示的结构来实现已知的接口(interface)类型吗?例如funcassertFoo(spec*ast.TypeSpec)bool{//spec.Name=="MyStruct"st,_:=spec.Type.(*ast.StructType)//Iwanttoknowwhether"MyStruct"implements"FooInterface"ornot_,ok:=st.Interface().(FooInterface)returnok}但是没有*ast.StructType.Interface():(