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

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题