草庐IT

INTERFACE

全部标签

go - 将结构数组传递给 Go 中的函数

我有一个从一些XML文件中解析出来的对象。它有这样的结构类型typeReportstruct{Items[]Item`xml:......`AnotherItems[]AnotherItem`xml:......`}typeItemstruct{Namestring}typeAnotherItemstruct{Namestring}func(Item*Item)Foo()bool{//somecodehere}func(AnotherItem*AnotherItem)Foo()bool{//anothercodehere}对于每个项目我都必须这样做:funcmain(){//somef

go - 如何从 Go 中的结构中的接口(interface)实例获取属性

我想获取v.val,但是go编译器抛给我一个错误:v.valundefined(typetestInterfacehasnofieldormethodval)但是在v.testMe方法中,它起作用了。packagemainimport("fmt")typetestInterfaceinterface{testMe()}typeoriValuestruct{valint}func(ooriValue)testMe(){fmt.Println(o.val,"I'mtestinterface")}funcmain(){varvtestInterface=&oriValue{val:1,}//

Go - 如何复制接口(interface)的 slice ?

我正在尝试使用辅助数组实现简单的合并排序。我有typebyString[]string实现了Less、Swap和Len方法。它基本上遵循Go的sort包的接口(interface)。但是,我在选择将byStringslice复制到临时数组的最佳路径时遇到了一些困难。请帮助我摆脱Java的多态性世界,使其与Go一起工作。funcmerge(dataInterface,lo,mid,hiint){i,j:=lo,mid+1//HowdoIcopydata'selementstoanewslicecalledaux?} 最佳答案 使用内置

json - 接口(interface)转换: interface is map[string]interface {} not

我对Go中的所有不同类型感到非常困惑,但我有一个严格定义的结构“VMR”,我正在尝试将数据转换为它。我正在查询CouchDB(使用GoSDK),然后尝试将返回的数据断言到我的结构中。当然,这是行不通的,它引发了panic。我在黑暗中拍摄,试图找出我做错了什么。这是我的函数/结构:typeVMRstruct{Namestring`json:"name,omitempty"`InUsebool`json:"inuse"`Descriptionstring`json:"description,omitempty"`Viewstring`json:"view,omitempty"`Themes

testing - 如何重写此 select 语句以保证 100% 的测试覆盖率?

这让我疯狂。假设我有以下功能:funcMap(quit对于从src接收到的每个值v,它在dst上发送f(v),直到src或quit关闭且为空或从quit接收到值。现在,假设我想编写一个测试来证明它可以被取消:funcTestMapCancel(t*testing.T){varwgsync.WaitGroupquit:=make(chanstruct{})success:=make(chanstruct{})wg.Add(3)src:=//channelprovidingarbitraryvaluesuntilquitiscloseddst:=make(chaninterface{})/

mysql - 类型接口(interface)的 channel 在带有 MySql 的 golang 中不接收值

我是golang的新手。我正在尝试使用golang对mysqldb进行并发查询。我知道channel可以是接口(interface)类型。当我在RunQuery函数中打印tableData(typemap)时,我得到了结果。我正在将tableData发送到ch,即接口(interface)类型的channel。在函数getdataList中,我没有在ch中获得任何值。我不明白我做错了什么。以下是我的代码:packagemainimport("database/sql""fmt""net/http"_"github.com/go-sql-driver/mysql""log")vardb*

go - 这是在 golang 中类型转换的吗?

paxPayment,ok=dataObject.(*entities.PassengerPayment)括号是做什么用的?我不确定这个赋值操作是怎么回事。您需要更多详细信息来回答这个问题吗? 最佳答案 这是一个Typeassertion.类型断言可用于:从接口(interface)类型的值中获取具体类型的值或获取与初始接口(interface)类型不同的接口(interface)类型的值(具有不同methodset的接口(interface),实际上不是原始接口(interface)的子集,因为可以使用简单的简单方法获得输入con

google-app-engine - 通过将 key 存储到 session golang 中来更快地加载页面

我正在尝试更快地加载动态页面。我正在将Twitter克隆作为一项学习任务。我正在遵循以下方法当有人发推文时,将推文存储在数据存储中,并在内存缓存中对其进行保护{key.string(),json.Marshal(tweet)}我在用户主页时间线中推送推文。主页时间线是一个[]*datastore.Key,它存储在用户session中(先复制到内存缓存中,然后再复制到数据库中)。当用户打开她的主页时,主页会尝试从session中获取key,如果找不到则进行数据存储查询。一旦我获得key,我就从内存缓存中获取推文(如果不是,则从数据库中获取)我卡在了第3步。在第一种情况下,我得到了正确的信

将 interface{} 转换为数字

我正在玩围棋,但无法解决这个问题。这是我想做的一个人为的例子。基本上,我在interface{}数据类型中保存了数字,我想编写用户指定大小的数字的二进制表示。funcmain(){buf:=new(bytes.Buffer)//importedfromencoding.binaryvarval1uint64=0xff//8bytesvarval2float32=9.876//4bytesvarainterface{}=val1varbinterface{}=val2//forargumentsake,letsassumewewanttowriteaasauint8//toourbina

elasticsearch - Golang : Using ElasticSearch library called Goes, 如何为 bool should 方法编写可执行代码?

我正在使用gos库(https://github.com/OwnLocal/goes),它是Golang中ElasticSearch的包装器。在ElasticSearch查询中,我们可以这样运行:{"query":{"match":{"user_id_1":"438018"}}}而且有效。对于使用gos的golang,你可以像这样运行它:varquery=map[string]interface{}{"query":map[string]interface{}{"match":map[string]interface{}{"user_id_1":"438018",},},}这是我的问题