问题标签 [type-assertion]

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 投票
1 回答
214 浏览

go - 指向切片的指针的 Golang 类型断言

有没有更好的方法呢?

play.golang.org上的完整示例

0 投票
1 回答
5812 浏览

go - Golang - [...] 接口的总和{}

我创建了一个通用数据结构,在 Golang 中有一个名称和一个通用数组。

现在我想定义一个方法,它允许我对数组中的所有值求和。我知道它们是数字的(尽管它们是否int依赖float于上下文)但我遇到了一些严重的麻烦。

不过,我似乎无法完成这项工作。当我取消注释第 18 和 27 行(fmt.Println(int_arr.Sum()fmt.Println(int_arr.Sum())并尝试运行我得到的代码时

编译期间。

有谁知道如何添加泛型类型,因为我们知道它们是数字的?

谢谢!

0 投票
1 回答
132 浏览

angular - 对象和原始值之间的类型断言失败

我已经创建了这个界面。

我打算创建一个名为 options 的变量,它允许“Option”数组或单个值这样的字符串。

我没有任何问题给变量赋值。

但是当我尝试使用该变量时,出现以下错误:

类型“字符串”上不存在属性“值”| 选项'。“字符串”类型上不存在属性“值”

我尝试验证该值是对象还是字符串。

还有类型断言,但我仍然遇到同样的错误。

我做了一些研究,但我没有找到这种情况的解决方案或解决方法。

0 投票
1 回答
1095 浏览

go - 从接口更改为所需类型

我有这个方法,我收到一个int64参数。该参数在某些领域使用,然后应该被传递给另一个需要不同类型的方法(来自外部库):type AcctInterimInterval uint32

我尝试将其转换为 auint32但脚本抱怨它:invalid type assertion: ... (non-interface type int on left).

我也尝试将其转换为,AcctInterimInterval但这次出现了不同的错误:interface conversion: interface {} is int, not main.AcctInterimInterval

到目前为止,这是我的测试代码:

相关游乐场: https: //play.golang.org/p/tTW5J2FIAy3

0 投票
1 回答
941 浏览

arrays - Golang 将 interface{} 转换为 N 大小的数组

T我有一个包含在接口中的数组。我事先知道数组的大小。如何编写一个通用函数来获取任何数组长度的数组(或切片)?例如,对于 3 号,我想要类似的东西

我不能真正进行类型切换,因为[1]byte它与[2]byteetc 不同,所以我必须明确枚举所有可能的大小。

0 投票
2 回答
859 浏览

go - reflect.TypeOf 的反转

我想找回我曾经保存过的值的类型。我使用了 reflect.Typeof() 并保存了类型。然后尝试使用开关类型。类型将始终为“*reflect.rtype”。我无法通过类型断言检索。

游乐场: https: //play.golang.org/p/kqDo4DPYjra

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 回答
734 浏览

go - 类型将自定义类型断言为基本类型

是否可以将自定义类型断言为其基本类型?

例如,如果我有以下

然后,如果我尝试通过键入 assert ,则会y.([]interface{})收到错误消息//interface {} is A, not []interface {}

编辑:正如下面的答案所指出的,我可以断言 A 通过y.(A). 问题源于xMongo驱动程序。有些驱动实现了自己的类型,例如官方的 mongo 驱动实现了bson.A类型的[]interface类型。如果我切换驱动程序,那么我的类型断言需要更改以匹配它们的自定义类型,这是我想要避免的。

0 投票
2 回答
174 浏览

arrays - 有没有更简单的方法在 Go 中解码这个 json?

我正在尝试将一些 JSON 从 Jira 解析为变量。这是使用 go-jira 包(https://godoc.org/github.com/andygrunwald/go-jira

目前我有一些代码可以让开发人员:

dev := jiraIssue.Fields.Unknowns["customfield_11343"].(map[string]interface{})["name"]

team := jiraIssue.Fields.Unknowns["customfield_12046"].([]interface{})[0].(map[string]interface{})["value"]

获得他们所属的团队。

让他们所在的团队有点恶心,除了必须输入断言,设置索引,然后再次输入断言之外,还有更简洁的方法来获得团队吗?

这是完整的json(修改但结构相同,太长了):

谢谢

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开始,有没有一种优雅的方式来做到这一点?

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