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 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划