dqzow3859 2017-12-06 11:33
浏览 97
已采纳

关于golang中的“ for”语法,在描述编译错误时是否使用了错误的术语?

I tried to use something like

for i := 0; i < len(bytes); ++i {
        ...
}

It is not correct and I got an error

syntax error: unexpected ++, expecting expression

It was because of ++i is not an expression I thought.

Then I found out that i++ (it works in for loop) is not an expression as well according to the documentation.

Also I met that in some cases (now I think in all cases) a statement can not be used instead of expression.

Now if we come back to the error we see that for loop requires an expression. I was confused with that. I checked one more part of the documentation it turns out for requires a statement.

For statements with for clause

A "for" statement with a ForClause is also controlled by its condition, but additionally it may specify an init and a post statement

I started with question (which I liked more than the final question because it was about language non-acquaintance as I thought)

Is it special case for loop syntax that statement are accepted as expression or are there other cases in golang?

During writing the question and checking the documentation I end up to a questions

Is there used incorrect terminology in description of the error that should be fixed not to confuse? Or is it normally in some cases to substitute such terms as statement and expression?

  • 写回答

2条回答 默认 最新

  • douzhang2092 2017-12-06 13:48
    关注

    The Go Programming Language Specification

    Primary expressions

    Primary expressions are the operands for unary and binary expressions.

    PrimaryExpr =
      Operand |
      Conversion |
      PrimaryExpr Selector |
      PrimaryExpr Index |
      PrimaryExpr Slice |
      PrimaryExpr TypeAssertion |
      PrimaryExpr Arguments .
    
    Selector       = "." identifier .
    Index          = "[" Expression "]" .
    Slice          = "[" [ Expression ] ":" [ Expression ] "]" |
                     "[" [ Expression ] ":" Expression ":" Expression "]" .
    TypeAssertion  = "." "(" Type ")" .
    Arguments      = "(" [ ( ExpressionList | Type [ "," ExpressionList ] ) [ "..." ] [ "," ] ] ")" .
    

    Operators and punctuation

    The following character sequences represent operators:

    ++    
    --    
    

    Operators

    Operators combine operands into expressions.

    Expression = UnaryExpr | Expression binary_op Expression .
    UnaryExpr  = PrimaryExpr | unary_op UnaryExpr .
    
    binary_op  = "||" | "&&" | rel_op | add_op | mul_op .
    rel_op     = "==" | "!=" | "<" | "<=" | ">" | ">=" .
    add_op     = "+" | "-" | "|" | "^" .
    mul_op     = "*" | "/" | "%" | "<<" | ">>" | "&" | "&^" .
    
    unary_op   = "+" | "-" | "!" | "^" | "*" | "&" | "<-" .
    

    Operator precedence

    The ++ and -- operators form statements, not expressions.

    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 ( "++" | "--" ) .
    

    ++ and -- are operators. The ++ and -- operators form statements, not expressions.

    IncDecStmt = Expression ( "++" | "--" ) . 
    

    When the compiler encounters an ++ operator, it expects it to be immediately preceded by an expresssion.

    For example,

    package main
    
    func main() {
    
        // syntax error: unexpected ++, expecting expression
        for i := 0; i < 1; ++i {}
    }
    

    Playground: https://play.golang.org/p/y2d9ijeMdw

    Output:

    main.go:6:21: syntax error: unexpected ++, expecting expression
    

    The compiler complains about the syntax. It found a ++ operator without an immediately preceding expression: syntax error: unexpected ++, expecting expression.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 深度学习残差模块模型
  • ¥20 两个不同Subnet的点对点连接
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)