草庐IT

interface-design

全部标签

interface - go中能够包含任意类型的数据结构是什么?

引用GoequivalentofavoidpointerinC这是一个演示。packagemainimport("fmt")funcmap_exist(map_valmap[string]interface{},keystring)bool{_,ok:=map_val[key]returnok}funcmain(){varonemap[string][]stringone["A"]=[]string{"a","b","c"}fmt.Println(map_exist(one,"A"))vartwomap[string]stringtwo["B"]="a"fmt.Println(map_

interface - go中能够包含任意类型的数据结构是什么?

引用GoequivalentofavoidpointerinC这是一个演示。packagemainimport("fmt")funcmap_exist(map_valmap[string]interface{},keystring)bool{_,ok:=map_val[key]returnok}funcmain(){varonemap[string][]stringone["A"]=[]string{"a","b","c"}fmt.Println(map_exist(one,"A"))vartwomap[string]stringtwo["B"]="a"fmt.Println(map_

Go heap.Interface 作为一个结构

我正在使用Go的heap创建一个优先级队列包裹。有anexample文档中的一个。我正在创建的队列需要基于结构而不是slice,因为它需要其他属性,如互斥量。typePQueuestruct{queue[]*Itemsync.Mutex}我实现了heap.Interface的所有方法需要。问题是我的PQueue.Push方法似乎没有永久地向PQueue.queue添加值。func(pPQueue)Push(xinterface{}){p.Lock()deferp.Unlock()item:=x.(*Item)item.place=len(p.queue)//theindexofanit

Go heap.Interface 作为一个结构

我正在使用Go的heap创建一个优先级队列包裹。有anexample文档中的一个。我正在创建的队列需要基于结构而不是slice,因为它需要其他属性,如互斥量。typePQueuestruct{queue[]*Itemsync.Mutex}我实现了heap.Interface的所有方法需要。问题是我的PQueue.Push方法似乎没有永久地向PQueue.queue添加值。func(pPQueue)Push(xinterface{}){p.Lock()deferp.Unlock()item:=x.(*Item)item.place=len(p.queue)//theindexofanit

go - 类型断言失败,从 golang 中的 interface{} 正确转换

这是我从下面的简单重现案例中得到的输出:2015/06/2221:09:50ok:false2015/06/2221:09:50stub:*main.Stub很明显,stub被正确标记为指向stub类型的指针,但转换失败。我正在尝试更新数组的内容。packagemainimport"log"constBUFFER_SIZE=8typeValuestruct{valueint}func(vValue)Value()int{returnv.value}func(v*Value)SetValue(valueint){v.value=value}typeStubstruct{Valuetest

go - 类型断言失败,从 golang 中的 interface{} 正确转换

这是我从下面的简单重现案例中得到的输出:2015/06/2221:09:50ok:false2015/06/2221:09:50stub:*main.Stub很明显,stub被正确标记为指向stub类型的指针,但转换失败。我正在尝试更新数组的内容。packagemainimport"log"constBUFFER_SIZE=8typeValuestruct{valueint}func(vValue)Value()int{returnv.value}func(v*Value)SetValue(valueint){v.value=value}typeStubstruct{Valuetest

go - 从 golang 中的字符串创建 bufio 接口(interface)

我想在golang中为一个在构造函数中接受io.Reader的结构编写单元测试。通常io.Reader接口(interface)来自TCP连接。现在我想使用预定义的字符串并将其用作io.Reader接口(interface)的输入。类似于:s:="thisismyinput"b:=io.NewReader(s)t:=NewTestStruct(b)t.doSomething() 最佳答案 strings.Reader实现io.Reader界面。您可以使用strings.NewReader构建它的新实例:s:="thisismyinp

go - 从 golang 中的字符串创建 bufio 接口(interface)

我想在golang中为一个在构造函数中接受io.Reader的结构编写单元测试。通常io.Reader接口(interface)来自TCP连接。现在我想使用预定义的字符串并将其用作io.Reader接口(interface)的输入。类似于:s:="thisismyinput"b:=io.NewReader(s)t:=NewTestStruct(b)t.doSomething() 最佳答案 strings.Reader实现io.Reader界面。您可以使用strings.NewReader构建它的新实例:s:="thisismyinp

go - 解码为接口(interface)类型

我有一些代码被丢弃了,实际上我被难住了——我以前使用过RPC和JSON方面的东西,但是当它在本地工作正常时,我似乎无法让它在RPC上工作。packagemainimport("log""net""net/rpc""net/rpc/jsonrpc""reflect")typeFoointerface{SayHello()error}typefakeFoostruct{internalValuestring}funcNewFakeFoo()*fakeFoo{f:=&fakeFoo{}f.internalValue="123456789012347"returnf}func(m*fakeFo

go - 解码为接口(interface)类型

我有一些代码被丢弃了,实际上我被难住了——我以前使用过RPC和JSON方面的东西,但是当它在本地工作正常时,我似乎无法让它在RPC上工作。packagemainimport("log""net""net/rpc""net/rpc/jsonrpc""reflect")typeFoointerface{SayHello()error}typefakeFoostruct{internalValuestring}funcNewFakeFoo()*fakeFoo{f:=&fakeFoo{}f.internalValue="123456789012347"returnf}func(m*fakeFo