zoukankan      html  css  js  c++  java
  • Golang的Semicolons

    Semicolons

    The formal grammar uses semicolons ";" as terminators in a number of productions. Go programs may omit most of these semicolons using the following two rules:

    1. When the input is broken into tokens, a semicolon is automatically inserted into the token stream at the end of a non-blank line if the line's final token is

    2. To allow complex statements to occupy a single line, a semicolon may be omitted before a closing ")" or "}".

    To reflect idiomatic use, code examples in this document elide semicolons using these rules.

    说得很清楚:

    Golang编译器自动在行尾插入分号(Semicolons):

        1. 关键字keywords

        2. 标识符identifiers

        3. 直接量Literals

        4. 某些运算符: ++, --, ), ], }

    由于在初始化块{...}中使用逗号而不是分号, 所以在块内换行需要谨慎编译器会自动插入分号.

    var files = []struct { Name, Body string }{ {"readme.txt", "This archive contains some text files."}, {"gopher.txt","Gopher names: George Geoffrey Gonzo"}, {"todo.txt", "Get animal handling license."}

    }

    编译时会报错, 原因是在最后一个元素后自动插入了分号, 解决办法:

        1. 在最后一个元素后也加逗号

    var files = []struct { Name, Body string }{ {"readme.txt", "This archive contains some text files."}, {"gopher.txt","Gopher names: George Geoffrey Gonzo"}, {"todo.txt", "Get animal handling license."},

    }

        2. 最后"}"不换行

    var files = []struct {

    Name, Body string }{ {"readme.txt", "This archive contains some text files."}, {"gopher.txt", "Gophernames: George Geoffrey Gonzo"}, {"todo.txt", "Get animal handling license."}}

  • 相关阅读:
    BZOJ1036 [ZJOI2008]树的统计Count 树链剖分
    SPOJ-QTREE Query on a tree 树链剖分
    BZOJ3224 洛谷3369 Tyvj 1728 普通平衡树 splay
    BZOJ1008 [HNOI2008]越狱 快速幂
    HDU4686 Arc of Dream 矩阵
    POJ2065 SETI 高斯消元
    POJ1487 Single-Player Games 高斯消元
    HDU3306 Another kind of Fibonacci 矩阵
    Hadoop基本操作
    Hadoop命令大全
  • 原文地址:https://www.cnblogs.com/zolo/p/5849142.html
Copyright © 2011-2022 走看看