2014-09-12 12 views
9
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 

nach der Kompilierung, erhalte ich die FehlermeldungGo golang, Syntaxfehler: unerwartetes ++, erwartet:

./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 : 

warum kann ich nicht i der Scheibe ++ in Index setzen? Gibt es irgendeine Einschränkung im Index der Scheibe?

Antwort

15

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 

schreiben,

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

, die in Ihrem speziellen Fall zu vereinfachen,

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

1. Sie können 'i ++' einfach gut verwenden, keine Notwendigkeit für "i + = 1". 2. Für diesen spezifischen Code ist es absolut nicht notwendig, "i" zu verwenden. http://play.golang.org/p/6h1kMGl8xe – OneOfOne

+0

Danke, zu verstehen. – HardySimpson

9

Nach Language Specification, http://golang.org/ref/spec#IncDec_statements, i ++ ist ein IncDec statements, die ein statement ist, aber kein expression .Als für args[index], muss Index ein expression sein. Sie wollen mehr Details, lesen Sie es einfach Go Language Specification, es ist genau das, was die Sprache verlangt.

5

Wie andere Leute haben gesagt, i++ eine Aussage in gehen, kein Ausdruck, wie es ist in C. Go hat eine andere Möglichkeit, dieselbe Absicht mit mehreren Zuweisungen auszudrücken:

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

Ihre Definition von map wäre auch zur Laufzeit fehlgeschlagen.

+0

Nett gesetzt - dachte nicht, es auf diese Weise zu versuchen – notzippy

Verwandte Themen