有没有办法将地图附加到切片?

Luk*_*kas 0 go slice

我正在用 Golang 制作一种新的编程语言。对于我的词法分析器,我想将我的标记映射附加到我的切片中。例如:

var tokens []map[string]string
tokens = append(tokens, {"type": "number", "value": "123"})
Run Code Online (Sandbox Code Playgroud)

我不断收到此错误:syntax error: unexpected {, expecting expression

Mar*_*arc 5

要使其成为有效的表达式,您需要指定文字的类型:

tokens = append(tokens, map[string]string{"type": "number", "value": "123"})
Run Code Online (Sandbox Code Playgroud)