英文:
Go golang, syntax error: unexpected ++, expecting :
问题
func test(args ...string) {
var msg map[string]interface{}
i := 0
msg["product"] = args[i]
i++
msg["key"] = args[i]
i++
msg["signature"] = args[i]
i++
msg["string_to_sign"] = args[i]
}
编译后,我得到以下错误信息:
./utils.go:28: 语法错误:意外的++,期望:
./utils.go:28: 标签后缺少语句
./utils.go:29: 语法错误:意外的++,期望:
./utils.go:30: 语法错误:意外的++,期望:
./utils.go:31: 语法错误:意外的++,期望:
./utils.go:36: 语法错误:意外的++,期望:
./utils.go:37: 语法错误:意外的++,期望:
为什么我不能在切片的索引中使用i++
?在切片的索引中有什么限制吗?
英文:
func test(args ...string) {
var msg map[string] interface{}
i := 0
msg["product"] = args[i++]
msg["key"] = args[i++]
msg["signature"] = args[i++]
msg["string_to_sign"] = args[i++]
}
go build utils.go
after compile, I get the error message
./utils.go:28: syntax error: unexpected ++, expecting :
./utils.go:28: missing statement after label
./utils.go:29: syntax error: unexpected ++, expecting :
./utils.go:30: syntax error: unexpected ++, expecting :
./utils.go:31: syntax error: unexpected ++, expecting :
./utils.go:36: syntax error: unexpected ++, expecting :
./utils.go:37: syntax error: unexpected ++, expecting :
why can't I put i++ in index of slice? is there any limitation in index of slice?
答案1
得分: 70
> Go常见问题(FAQ)
>
> 为什么++和--是语句而不是表达式?为什么是后缀而不是前缀?
>
> 没有指针算术,前缀和后缀递增运算符的方便性降低了。通过将它们从表达式层次结构中完全移除,表达式语法得到简化,同时也消除了关于++和--的求值顺序的混乱问题(考虑f(i++)和p[i] = q[++i])。这种简化是显著的。至于后缀和前缀,两者都可以正常工作,但后缀版本更加传统;对前缀的坚持起源于STL,这是一个名字中讽刺地包含后缀递增的语言的库。
>
> Go编程语言规范
>
> 递增和递减语句
>
> “++”和“--”语句通过未类型化的常量1递增或递减它们的操作数。与赋值一样,操作数必须是可寻址的或映射索引表达式。
>
> IncDecStmt = Expression ( "++" | "--" ) .
>
> 以下赋值语句在语义上是等价的:
>
> 递增递减语句 赋值语句
> x++ x += 1
> x-- x -= 1
写成:
func test(args ...string) {
var msg map[string]interface{}
i := 0
msg["product"] = args[i]
i++
msg["key"] = args[i]
i++
msg["signature"] = args[i]
i++
msg["string_to_sign"] = args[i]
}
在你的特定情况下,简化为:
func test(args ...string) {
var msg map[string]interface{}
msg["product"] = args[0]
msg["key"] = args[1]
msg["signature"] = args[2]
msg["string_to_sign"] = args[3]
}
英文:
> Go Frequently Asked Questions (FAQ)
>
> Why are ++ and -- statements and not expressions? And why postfix,
> not prefix?
>
> Without pointer arithmetic, the convenience value of pre- and postfix
> increment operators drops. By removing them from the expression
> hierarchy altogether, expression syntax is simplified and the messy
> issues around order of evaluation of ++ and -- (consider f(i++) and
> p[i] = q[++i]) are eliminated as well. The simplification is
> significant. As for postfix vs. prefix, either would work fine but the
> postfix version is more traditional; insistence on prefix arose with
> the STL, a library for a language whose name contains, ironically, a
> postfix increment.
>
> The Go Programming Language Specification
>
> IncDec statements
>
> The "++" and "--" statements increment or decrement their operands by
> the untyped constant 1. As with an assignment, the operand must be
> addressable or a map index expression.
>
> IncDecStmt = Expression ( "++" | "--" ) .
>
> The following assignment statements are semantically equivalent:
>
> IncDec statement Assignment
> x++ x += 1
> x-- x -= 1
Write,
func test(args ...string) {
var msg map[string]interface{}
i := 0
msg["product"] = args[i]
i++
msg["key"] = args[i]
i++
msg["signature"] = args[i]
i++
msg["string_to_sign"] = args[i]
}
Which, in your particular case, simplifies to,
func test(args ...string) {
var msg map[string]interface{}
msg["product"] = args[0]
msg["key"] = args[1]
msg["signature"] = args[2]
msg["string_to_sign"] = args[3]
}
答案2
得分: 20
根据语言规范,http://golang.org/ref/spec#IncDec_statements,i++ 是一个“IncDec语句”,它是一个“语句”,而不是一个“表达式”。至于 args[index]
,index 必须是一个“表达式”。如果你想了解更多细节,只需阅读Go语言规范,这正是语言要求的。
英文:
According to Language Specification, http://golang.org/ref/spec#IncDec_statements, i++ is a IncDec statements
, which is a statement
, but not a expression
.As for args[index]
, index must be a expression
. You want more details , just read it Go Language Specification, it's just what the language demand.
答案3
得分: 14
正如其他人所说,i++
在 Go 中是一个语句,而不是像在 C 中那样是一个表达式。Go 有一种不同的方式来表达相同的意图,即使用多重赋值:
func test(args ...string) {
msg := make(map[string]string)
i := 0
msg["product"], i = args[i], i+1
msg["key"], i = args[i], i+1
msg["signature"], i = args[i], i+1
msg["string_to_sign"], i = args[i], i+1
fmt.Printf("%v\n", msg)
}
你对 map
的定义在运行时也会失败。
英文:
As other people have said i++
is a statement in go, not an expression as it is in C. Go has a different way of expressing the same intent using multiple assignment:
func test(args ...string) {
msg := make(map[string]string)
i := 0
msg["product"], i = args[i], i+1
msg["key"], i = args[i], i+1
msg["signature"], i = args[i], i+1
msg["string_to_sign"], i = args[i], i+1
fmt.Printf("%v\n", msg)
}
Your definition of map
would have failed at runtime too.
答案4
得分: 5
++
运算符令人失望。这是我的修改:
func test(args ...string) {
i := 0
inc := func(i *int) int { *i++; return *i }
var msg map[string]interface{}
msg["product"] = args[inc(&i)]
msg["key"] = args[inc(&i)]
msg["signature"] = args[inc(&i)]
msg["string_to_sign"] = args[inc(&i)]
}
英文:
++
operator is disappointing. This is my hack:
func test(args ...string) {
i := 0
inc := func(i *int) int { *i++; return i }
var msg map[string] interface{}
msg["product"] = args[inc(&i)]
msg["key"] = args[inc(&i)]
msg["signature"] = args[inc(&i)]
msg["string_to_sign"] = args[inc(&i)]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论