Go golang, syntax error: unexpected ++, expecting :

后端 未结 4 836
醉梦人生
醉梦人生 2021-02-01 13:10
  func test(args ...string) {
    var msg map[string] interface{}

    i := 0
    msg[\"product\"] = args[i++]
    msg[\"key\"] = args[i++]
    msg[\"signature\"] = args         


        
相关标签:
4条回答
  • 2021-02-01 13:40

    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.

    0 讨论(0)
  • 2021-02-01 13:51

    ++ 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)]
      }
    
    0 讨论(0)
  • 2021-02-01 13:52

    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.

    0 讨论(0)
  • 2021-02-01 14:03

    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]
    }
    
    0 讨论(0)
提交回复
热议问题