问题标签 [go-interface]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
800 浏览

go - Golang plugin type assertion

I'm writing a simple app which loads plugin in a predefined format. Example plugin is the following:

I can't quite apprehend how to load such a structure in my main program. So, I declare an interface:

Then I load "getter" function and try to cast it to a function returning interface to then call it:

But the cast fails with an error:

If I return an actual instance (not a pointer) from GetProcessor and try to cast the function to the one returning Processor, I get the same result:

How to get a struct instance from plugin (therefore load the function returning it) and type-assert it's an expected interface in my case?

UPD: If I remove everything from Processor interface (that is, it becomes just an empty interface):

And try to cast procGetterInter to a function returning a pointer to Processor interface:

I still get the same error:

Why doesn't it cast even to pointer to an empty interface?

0 投票
2 回答
100 浏览

string - 这两个err值有什么区别?

我正在尝试理解 golang 接口,我的问题是为什么 err2.What 未定义。

这是一个简单的代码。输出表明 err 和 err2 与 *main.MyError 具有相同的类型,但 err2 没有字段“What”,因此 err 和 err2 之间肯定存在一些差异,但我无法在这里弄清楚差异。我刚开始学习golang不久,任何帮助将不胜感激。

预期输出:

main.MyError
Hello
main.MyError
it didn't work

实际输出:

\# command-line-arguments ./test.go:34:18: err2.What undefined (type error has no field or method What)

0 投票
1 回答
1624 浏览

go - 如何将 nil 接口转换为 nil 其他接口

我有一个经典的 Go nil 界面问题。

我正在尝试将interface{}我从 a 分配的 a断言nil errorerror接口。这句话令人困惑,所以我有一个方便的例子: https: //play.golang.com/p/Qhv7197oIE_z

输出:

所以换句话说,我试图从一个向下转换nil interface{}到一个nil error接口。如果我知道它interface{}被分配为一nil error开始,有没有一种优雅的方式来做到这一点?

仅供参考,如果这看起来不寻常,那是因为我正在实施一些模拟测试。

0 投票
0 回答
130 浏览

go - 迭代和更新 map[string]interface{} 值的属性

我们有一个如下所示的接口对象,数据如下所示

[地图[com.org.exts.criteria.model.Sourceemployee:map[active:true ids:map[com.org.exts.criteria.model.employee.ids:map[alternateIds:alternateSubId:map[string:SomeValuestring] IDnumber: somefiledAssignedIds: familyId:map[int:111] MyAccountIds: ABCNumbers: OldIds: nrtNumbers: GHTNumb: Somefiledx: Somefiledx: Somefiledz:map[string:OldId1] somefiled779: someIds: somenumberfiled: someoriginid: someconsumerids:]

我可以将接口转换为 map[string]interface{},但是当我们这样做时,结构会嵌套在地图中。遍历接口对象中的所有属性最好的方法是什么?

我们可以将接口转换为上面代码中提到的 map[string]interface{},但无法动态导航与地图嵌套。尝试动态遍历地图中的每个属性并能够对其进行更新。

0 投票
1 回答
1209 浏览

go - 以编程方式填充 golang 结构

我有一个包含多种类型数据记录的文件,我需要将其解析为结构。

我会很感激学习一种惯用的方式——如果存在的话——按记录类型填充结构。类似于 python 的namedtuple(*fields)构造函数。

我特别有兴趣了解标记的行是否// 1可以// 2方便地被代码替换,也许是某种允许结构本身处理类型转换的通用解码器。

0 投票
1 回答
90 浏览

unit-testing - 如何使用相互依赖的接口方法模拟结构

我在 Go 中为一个相当常见的用例/模式编写单元测试时遇到了麻烦。

想象一下,如果你愿意的话,是这样的:

根据需要重复调​​用GetAllResources是一种常见的模式。GetResource

我可以使用gomockortestify来测试GetResource. 但是,在测试时GetAllResource,我想模拟GetResource. 否则,测试将成为一场噩梦。这就是在 Java 中easymockmockito在使用部分模拟的情况下的方式。但是,尚不清楚如何在 Golang 中实现相同的目标。

具体来说,我找不到如何部分模拟struct. 大多数建议都围绕着打破这样struct的 s 但在这种情况下,这struct已经是最低限度了。为了测试而不要将接口(分为单个和多个)打破似乎是一个公平的要求,ResourceManager因为这没有多大意义,而且充其量也是笨拙的,并且随着更多这样的方法进入接口,也不能很好地扩展。

0 投票
2 回答
182 浏览

go - 在正确的结构中转换 json 而不是使用接口

我正在努力为解组以下 json 创建数据结构:

如果我将以下结构与接口一起使用,则没有问题:

但我需要更严格的打字,所以我尝试过:

但不幸的是我没有成功。

这是我用于解析 Kraken API 以检索订单簿的代码:

我用于测试的数据如下:

编辑

注意:我在输入中收到的数据是我发布的最新 json,而不是bidsandasks

我试图整合@chmike 提出的解决方案。不幸的是,需要进行一些预处理,因为数据是我发布的最新 json。

因此,我已更改为以下代码,以提取与asksand相关的 json 数据bids

不幸的是,我收到以下错误:

json: cannot unmarshal array into Go value of type json.Number

0 投票
0 回答
139 浏览

go - Go 抽象和错误

假设我需要对具有方法的某种类型进行抽象,该方法Do返回一个error

我需要一个更好的架构来识别这是否是已知错误。我可以做到 java 风格,让实现知道我们的错误类型。例如

然而,在这样的架构中,我会强制实现 ( complex.Doer) 适应接口 ( do.Doer),根据Go Wiki,这不是接口应该被使用的方式。

在这种情况下处理错误的正确方法是什么?

0 投票
1 回答
1332 浏览

unit-testing - 如何使用 Go 的 moq 模拟 http.Handler?

我有兴趣创建一个httptest.Server简单地记录调用它的请求。为此,我想使用该github.com/matryer/moq库。

为了生成一个模拟http.Handler,我将它的定义复制到一个名为的包中client

然后我跑了

client目录中,它产生了以下内容handler_mock.go

但是,如果我尝试运行以下命令client_test.go

我明白了

实际上,如果我查看ServeHTTP(来自https://github.com/golang/go/blob/c112289ee4141ebc31db50328c355b01278b987b/src/net/http/server.go#L2008-L2013)的实际定义,ServeHTTP()则没有指针接收器:

如何解决此问题以制作具有模拟处理程序函数的测试服务器?

0 投票
1 回答
136 浏览

go - 在 Go 中,如何检查接口是否实现了结构的所有导出方法?

我正在开发一个 Go 库,我在其中使用该interfacer工具(https://github.com/rjeczalik/interfaces)从结构创建接口,然后运行moqhttps://github.com/matryer /moq ) 为该接口生成一个模拟对象。现在我想编写一个单元测试,如果有人在没有更新接口和模拟的情况下将导出的方法添加到结构中,该测试将失败。

在高层次上,在我看来,我可以reflect.Value同时获取接口和结构并调用NumMethod()它们,然后检查数字是否相等。然而,我正在努力实现这个想法。

例如,如果我尝试这个:

我可以调用 get the number of methods person,但不是 of the personInterface,因为这会因错误消息而恐慌

reflect: 在零值上调用 reflect.Value.NumMethod

这是完整的错误:

如何获得由 实现的方法的数量interface,更一般地说,我将如何检查接口是否实现了结构的所有导出方法?