草庐IT

interface-design

全部标签

接口(interface)和将匿名字段嵌入到结构中

当附近发生爆炸时,我试图代表一个戴着眼镜的人靠近window。main是爆炸期间应​​该做什么的草图。有些东西应该收集爆炸附近的物体列表,并为每个物体做特定的事情(例如粉碎或熔化)。玻璃和window如预期般碎裂,但出于某种原因,人也碎裂了。为什么?packagemainimport"fmt"typeHumanstruct{Glasses}typeGlassesstruct{}typeShatterableinterface{shatter()}func(gGlasses)shatter(){}typeWindowstruct{}func(wWindow)shatter(){}func

pointers - 作为函数参数的接口(interface)指针

这很可能源于对interface{}是什么的误解。我有以下代码typeConfigurationstruct{Usernamestring}funcloadJson(jsonStr[]byte,x*Configuration}){json.Unmarshal(jsonStr,x)}funcmain(){//varconfig*Configurationconfig:=new(Configuration)file,e:=ioutil.ReadFile("config.json")loadJson(file,config)fmt.Printf("%s\n",config.Username)

pointers - 作为函数参数的接口(interface)指针

这很可能源于对interface{}是什么的误解。我有以下代码typeConfigurationstruct{Usernamestring}funcloadJson(jsonStr[]byte,x*Configuration}){json.Unmarshal(jsonStr,x)}funcmain(){//varconfig*Configurationconfig:=new(Configuration)file,e:=ioutil.ReadFile("config.json")loadJson(file,config)fmt.Printf("%s\n",config.Username)

interface - Go 界面中的设计决策{}

为什么Go不会自动转换:packagemainimport"fmt"typeAnyinterface{}//AnyisanemptyinterfacetypeXfunc(xAny)//XisafunctionthatreceivesAnyfuncY(xX){//YisafunctionthatreceivesXx(1)}functest(vinterface{}){//testisnotconsideredequaltoXfmt.Println("called",v)}funcmain(){Y(test)//error:cannotusetest(typefunc(interface{

interface - Go 界面中的设计决策{}

为什么Go不会自动转换:packagemainimport"fmt"typeAnyinterface{}//AnyisanemptyinterfacetypeXfunc(xAny)//XisafunctionthatreceivesAnyfuncY(xX){//YisafunctionthatreceivesXx(1)}functest(vinterface{}){//testisnotconsideredequaltoXfmt.Println("called",v)}funcmain(){Y(test)//error:cannotusetest(typefunc(interface{

go - 理解 Go 中的接口(interface)

我想了解接口(interface)在Go中是如何工作的。假设我有2个结构:package"Shape"typeSquarestruct{edgesCountint}typeTrianglestruct{edgesCountint}现在我创建一个Shape界面:typeShapeinterface{}为什么我不能指定Shape接口(interface)有一个egdesCount属性?接口(interface)只应该重组方法吗?我面临的另一个问题是共享功能。不可能想出这样的事情:funcNew()*Shape{s:=new(Shape)s.edgesCount=0returns}这比必须重

go - 理解 Go 中的接口(interface)

我想了解接口(interface)在Go中是如何工作的。假设我有2个结构:package"Shape"typeSquarestruct{edgesCountint}typeTrianglestruct{edgesCountint}现在我创建一个Shape界面:typeShapeinterface{}为什么我不能指定Shape接口(interface)有一个egdesCount属性?接口(interface)只应该重组方法吗?我面临的另一个问题是共享功能。不可能想出这样的事情:funcNew()*Shape{s:=new(Shape)s.edgesCount=0returns}这比必须重

go - 接口(interface)引用显示奇怪的输出

我试图理解Go的接口(interface)概念并创建以下代码:packagemainimport"fmt"typeFailerinterface{Error()string}typeSuccerinterface{Success()string}typeResultinterface{FailerSuccer}typeFailstruct{}func(*Fail)Error()string{return"Error"}typeSuccstruct{}func(*Succ)Success()string{return"Success"}typeCombistruct{}func(*Com

go - 接口(interface)引用显示奇怪的输出

我试图理解Go的接口(interface)概念并创建以下代码:packagemainimport"fmt"typeFailerinterface{Error()string}typeSuccerinterface{Success()string}typeResultinterface{FailerSuccer}typeFailstruct{}func(*Fail)Error()string{return"Error"}typeSuccstruct{}func(*Succ)Success()string{return"Success"}typeCombistruct{}func(*Com

interface - 如何访问接口(interface)的属性

我打算在两个响应结构的header和正文中都使用HTTP状态代码。Bu没有将状态代码设置为函数参数两次,并且再次为结构避免冗余。JSON()的参数response是一个允许两种结构都被接受的接口(interface)。编译器抛出以下异常:response.Statusundefined(typeinterface{}hasnofieldormethodStatus)因为响应字段不能有状态属性。有没有另一种方法可以避免两次设置状态代码?typeResponsestruct{Statusint`json:"status"`Datainterface{}`json:"data"`}typeE