【发布时间】:2012-09-26 13:42:48
【问题描述】:
我有
type DocId int
func foo(documents []String) {
for i := range documents {
id := DocId(i)
...
}
}
如何摆脱显式转换线? DocIds 意味着是索引单个文档的类型。
我想要的更像:
func foo(documents []String) {
for id := range documents {
... // id gets used as the DocId that it damn well *IS*
}
}
当我尝试将 range 中的 id 用作 DocId 时,即使 DocId 是 int,这会给我“无效操作:...(不匹配的 int 和 DocId 类型)”。
【问题讨论】:
-
不,DocId 是文档数组的索引类型。
-
id gets used as the DocId that it damn well *IS*,但不是。 -
"DocId is an int" 不完全正确:Go 是键入的。
标签: type-conversion go