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

后端 未结 4 899
醉梦人生
醉梦人生 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.

提交回复
热议问题