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:
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}
- 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. "}}