草庐IT

interface-design

全部标签

casting - Go:将 map[string]interface{} 转换为 map[string]string 的类型失败

我不确定为什么以下转换不起作用:import"fmt"funcmain(){v:=map[string]interface{}{"hello":"world"}checkCast(v)}funccheckCast(vinterface{}){_,isCorrectType:=v.(map[string]string)if!isCorrectType{fmt.Printf("incorrecttype") 最佳答案 map[string]interface{}与map[string]string不同。interface{}类型与str

go - 期待 nil 但得到一个 nil 值的接口(interface)作为返回,这应该是 nil

运行时间https://play.golang.org/p/sl12vfS9vPpackagemainimport"fmt"funcmain(){err:=run()iferr!=nil{fmt.Printf("%#v",err)}}funcrun()(errerror){returncheck()}funccheck()*Result{returnnil}typeResultstruct{messagestring}func(result*Result)Error()string{returnresult.message} 最佳答案

go - 期待 nil 但得到一个 nil 值的接口(interface)作为返回,这应该是 nil

运行时间https://play.golang.org/p/sl12vfS9vPpackagemainimport"fmt"funcmain(){err:=run()iferr!=nil{fmt.Printf("%#v",err)}}funcrun()(errerror){returncheck()}funccheck()*Result{returnnil}typeResultstruct{messagestring}func(result*Result)Error()string{returnresult.message} 最佳答案

pointers - go - 写入接口(interface)的函数{}

我想将一个指向某个东西的指针传递给一个函数,在编译时不知道它的类型,让函数写入它。这是我认为可行的方法:funcfoo(destinterface{}){switch(dest).(type){case*int:fmt.Println("gotint")*dest=1//handleothercases...}}但是,使用*int输入调用它funcmain(){bar:=2foo(&bar)fmt.Println(bar)//expect1}产生编译错误dest的间接指令无效(类型接口(interface){})。我在这里做错了什么? 最佳答案

pointers - go - 写入接口(interface)的函数{}

我想将一个指向某个东西的指针传递给一个函数,在编译时不知道它的类型,让函数写入它。这是我认为可行的方法:funcfoo(destinterface{}){switch(dest).(type){case*int:fmt.Println("gotint")*dest=1//handleothercases...}}但是,使用*int输入调用它funcmain(){bar:=2foo(&bar)fmt.Println(bar)//expect1}产生编译错误dest的间接指令无效(类型接口(interface){})。我在这里做错了什么? 最佳答案

http - 将 interface{} 参数转换为 Go 中的 *http.Request 或 *http.Response

我打算创建一个util函数,它将读取请求/响应的主体并将其返回。这是我目前所做的:funcGetBody(ininterface{})[]byte{varbodyio.ReadervarstatusCodeintswitchv:=in.(type){case*http.Request,*http.Response:body=v.BodystatusCode=v.StatusCodedefault:log.Fatal("Onlyhttp.Requestandhttp.Responseparameterscanbeacceptedtoparsebody")}ifstatusCode!=20

http - 将 interface{} 参数转换为 Go 中的 *http.Request 或 *http.Response

我打算创建一个util函数,它将读取请求/响应的主体并将其返回。这是我目前所做的:funcGetBody(ininterface{})[]byte{varbodyio.ReadervarstatusCodeintswitchv:=in.(type){case*http.Request,*http.Response:body=v.BodystatusCode=v.StatusCodedefault:log.Fatal("Onlyhttp.Requestandhttp.Responseparameterscanbeacceptedtoparsebody")}ifstatusCode!=20

go - 使用可变参数实现接口(interface)方法

我从简单的界面开始:typeModuleinterface{Init(deps...interface{})error}我想,实现会非常简单,因为这个方法应该匹配任意数量的提供参数。这就是我最终得到的这段代码,我想,TestModule实现了Module接口(interface)。typeTestModulestruct{}func(m*TestModule)Init(strstring)error{returnnil}但是当我想将TestModule传递给任何需要Module的函数时,我得到这个错误:cannotusemodule(type*TestModule)astypeModu

go - 使用可变参数实现接口(interface)方法

我从简单的界面开始:typeModuleinterface{Init(deps...interface{})error}我想,实现会非常简单,因为这个方法应该匹配任意数量的提供参数。这就是我最终得到的这段代码,我想,TestModule实现了Module接口(interface)。typeTestModulestruct{}func(m*TestModule)Init(strstring)error{returnnil}但是当我想将TestModule传递给任何需要Module的函数时,我得到这个错误:cannotusemodule(type*TestModule)astypeModu

Go结构字段与接口(interface)方法同名

来自第三方:packagelibtypeBarinterface{Age()int}Foo(bBar)int这不会编译,因为Age既是方法名又是字段名:packagemainimport"lib"typePersonstruct{Ageint}func(p*Person)Age()int{returnp.Age}funcmain(){p:=Person()lib.Foo(p)}没有重命名Person.Age,有没有办法在Personlib.Foo()/? 最佳答案 嗯,当然不是直接的,原因已经说过了。但是您可以围绕Person创建一