接受地图参数

huangapple go评论67阅读模式
英文:

Accepting map parameters

问题

我有一个简单的函数,用于测试一个字符串是否为整数。

func testInt(str string, m map[bool]) int {
    _, e := strconv.ParseInt(str, 0, 64)
    return m[nil == e] * 7
}

其中传递的映射包含 m[true] = 1m[false] = 0。然而,当我尝试运行这段代码时,Go 报错:

1: 语法错误:意外的 )

要么我不能以这种方式将映射作为参数传递,要么我完全弄错了。

英文:

I have a simple function which tests whether a string is an integer

func testInt(str string, m map[bool]) int {
    _,e := strconv.ParseInt(str, 0, 64);
    return m[nil == e] * 7;
}

where the map being passed contains m[true] = 1, m[false] = 0. However, when I attempt to run this Go complains

1: syntax error: unexpected )

Either I cannot pass maps around as parameters in this way, or else I am doing this entirely wrong.

答案1

得分: 15

一个map将键映射到值,使用以下语法:

map[KeyType]ValueType

(参见https://blog.golang.org/go-maps-in-action)

在你的函数中,你没有指定一个ValueType,导致了这个语法错误。看起来你想要一个map[bool]int

英文:

A map maps keys to values, using the syntax

map[KeyType]ValueType

(see https://blog.golang.org/go-maps-in-action)

In your function, you have not specified a ValueType, causing this syntax error. It looks like you want a map[bool]int.

huangapple
  • 本文由 发表于 2015年3月5日 13:36:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/28870784.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定