Golang's semicolons

Source: Internet
Author: User
Tags gopher
This is a creation in Article, where the information may have evolved or changed.

Semicolons

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

    1. When the input was broken into tokens, a semicolon was automatically inserted into the token stream at the end of a Non-blan K Line if the line's final token is

      • An identifier
      • An integer, floating-point, imaginary, rune, or string literal
      • One of the keywords,, break continue fallthrough , orreturn
      • One of the operators and delimiters,,,, ++ -- ) ] or}

    2. To allow complex statements to occupy a, a semicolon is omitted before a closing ")" or "}" .

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

That's clear:

The Golang compiler automatically inserts a semicolon (semicolons) at the end of a line:

1. Keyword keywords

2. Identifier identifiers

3. Direct Volume literals

4. Some operators: + +,--,),],}

Because of the initialization block {...} Use commas instead of semicolons, so you need to be careful that the compiler inserts semicolons in the block.

Like

var files = []struct {Name, Body string} {{"Readme.txt", "This archive contains some text files."}, {"Gopher.txt", "Gophe R Names:\ngeorge\ngeoffrey\ngonzo "}, {" Todo.txt "," Get Animal handling License. "}

}

The error occurs at compile time because the semicolon is automatically inserted after the last element, and the workaround:

1. Add a comma after the last element

var files = []struct {Name, Body string} {{"Readme.txt", "This archive contains some text files."}, {"Gopher.txt", "Gophe R Names:\ngeorge\ngeoffrey\ngonzo "}, {" Todo.txt "," Get Animal handling License. "},

}

2. Last "}" does not break line

var files = []struct {

Name, Body string} {"Readme.txt", "This archive contains some text files."}, {"Gopher.txt", "Gophernames:\ngeorge\ngeoffrey\ngonzo"}, {" Todo.txt "," Get Animal handling License. "}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.