http://godoc.golangtc.com/doc/faq#inc_dec
簡單地說, 在golang中++,--操作是語句而不是表達式. 所以a=b++, return x++之類絕對提示錯誤. 語句是無法放到表達式的位置
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.