去golang,语法错误:意想不到的++,期待: [英] Go golang, syntax error: unexpected ++, expecting :

查看:318
本文介绍了去golang,语法错误:意想不到的++,期待:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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

编译后,我收到错误消息:

  ./utils.go:28:语法错误:意外的++,期望:
./utils.go:28:缺少标签后的语句
./utils.go:29:语法错误:意外++,期待:
./utils.go:30 :语法错误:意外++,期待:
./utils.go:31:语法错误:意外++,期待:
./utils.go:36:语法错误:意外++,期望:
./utils.go:37:语法错误:意外++,期待:



<为什么我不能把i ++放在切片的索引中?在切片的索引中是否有任何限制?

解决方案


常见问题解答



为什么是++和 - 语句而不是表达式?为什么postfix,
不是前缀?



如果没有指针算术,前缀和后缀
增量运算符的便利值会下降。通过将它们从表达式
全部删除,表达式语法被简化并且乱七八糟的
以评估++和 - (考虑f(i ++)和
p [i] = q [++ i])也被淘汰。简化是
显着。至于后缀与前缀,两者都可以正常工作,但
后缀版本更传统;坚持前缀与
一起产生了STL,一个语言库,其名称中含有讽刺意味的是
后缀增量。



Go编程语言规范





++和 - 语句将它们的操作数增加或减少
无类型常量1.与赋值一样,操作数必须是
addressable或map index表达式。 =表达式(++| - )。

以下赋值语句在语义上是等效的:

  IncDec语句赋值
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]
}

在您的特定情况下,它简化为


$ b $

  func test(args ... string){
var msg map [string] interface {}
msg [产品] = args [0]
msg [key] = args [1]
msg [signature] = args [2]
msg [string_to_sign] = args [3]
}


  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?

解决方案

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]
}

这篇关于去golang,语法错误:意想不到的++,期待:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆