The formal syntax specifies what is a valid program in the Go language (or other language) based on the syntax. ' Block = ' {' statementlist '} '. Statementlist = {Statement ";"}. "The above definition is taken from the Go specification. It uses the [extended Backus-naur] (https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form) Form (EBNF). This means that a block of code is one or more statements separated by semicolons. A function call is an example of an expression. We can create a simple block of code: "' go{fmt. PRINTLN (1); Fmt. PRINTLN (2);} "The seasoned Gophers should notice that the usual code does not use semicolons at the end of each line." It can be simplified to: ' ' {fmt. PRINTLN (1) fmt. PRINTLN (2)} "has the same code as the previous work. Now that the syntax requires a semicolon, what makes it work? # # root Cause! [] (Https://raw.githubusercontent.com/studygolang/gctt-images/master/automatic-semicolon-insertion-in-go/1.jpeg What is the reason that language designers are starting to get rid of tokens such as semicolons? The answer is quite simple. It's all about readability. The less artificial code, the easier it is to use. This is important because once a piece of code is written, it is likely to be read by different people. The syntax uses semicolons as the product terminator. Because the goal is for programmers to not have to type these semicolons, there must be a way to inject them automatically. This is the lexer of Go [what is being done] (https://github.com/golang/go/blob/1106512db54fc2736c7a9a67dd553fc9e1fca742/src/go/scanner/ scanner.go#l641). The semicolon is added when the last token of the line is one of the following: + one [identifier] (https://golang.org/ref/spec#Identifiers) + one [integer] (https://golang.org/ref/spec# Integer_literals), [floating point] (https://golang.org/ref/spec#Floating-point_literals), [Imaginary] (https://golang.org/ref/spec#Imaginary_literals), [symbol] (https://golang.org/ref/spec#Rune_literals) or [string] ( Https://golang.org/ref/spec#String_literals). + [keywords] (https://golang.org/ref/spec#Keywords) break, continure, Fallthrough or return+ [operator and delimiter] (https://golang.org/ Ref/spec#operators_and_delimiters) One of ++,--,),] or} Let's look at an example: "' Gofunc g () int {return 1}func f () func (int) {return func (n int) {FMT. Println ("Inner func called")} "has such a definition that we can analyze two situations: ' ' Gof () (g ()) ' and '" ' and ' Gof () ' (g ()) ' The first fragment does not print anything, but the second gives an intrinsic function call. This is because of the 4th rule mentioned earlier: Because the last token is a parenthesis, a semicolon is appended to both lines. "' Gof ();(g ()); ' # # on the bottom! [] (Https://raw.githubusercontent.com/studygolang/gctt-images/master/automatic-semicolon-insertion-in-go/2.jpeg Golang adds a semicolon when parsing (scanning). When the '. Go ' file starts, the characters are converted to identifiers, numbers, keywords, and so on. The scanner is implemented with Go, so we can easily use it: "' Gopackage mainimport (" FMT "" Go/scanner "" Go/token ") func Main () {scanner: = scanner. scanner{} Source: = []byte ("N: = 1\nfmt. PRINTLN (n) ") ErrorHandler: = Func (_ token. Position, msg string) {Fmt. Printf ("Error handler callEd:%s\n ", msg)} Fset: = token. Newfileset () File: = Fset. AddFile ("", Fset. Base (), Len (source)) scanner. Init (file, source, ErrorHandler, 0) for {position, Tok, literal: = Scanner. Scan () fmt. Printf ("%d:%s", position, Tok) if literal! = "" {fmt. Printf ("%q", literal)} FMT. Println () if Tok = = token. EOF {Break}} ' output: ' ' 1:ident ' n ' 3:: =6:int "1" 7:; "\ n" 8:ident "FMT" one by one:. 12:ident "Println": (20:ident "n" 21:) 22:; "\ n" 22:eof "line printing; "\ n" is where the scanner (lexer) adds a semicolon to the Prime Minister: "gon: = 1fmt. PRINTLN (n) "Golangspec has more than 300 followers. This is not the goal, but more and more people think it is a useful publication when it is very dynamic. Like add attention, double-click 666.
via:https://medium.com/golangspec/automatic-semicolon-insertion-in-go-1990338f2649
Author: Michałłowicki Translator: themoonbear proofreading: polaris1119
This article by GCTT original compilation, go language Chinese network honor launches
This article was originally translated by GCTT and the Go Language Chinese network. Also want to join the ranks of translators, for open source to do some of their own contribution? Welcome to join Gctt!
Translation work and translations are published only for the purpose of learning and communication, translation work in accordance with the provisions of the CC-BY-NC-SA agreement, if our work has violated your interests, please contact us promptly.
Welcome to the CC-BY-NC-SA agreement, please mark and keep the original/translation link and author/translator information in the text.
The article only represents the author's knowledge and views, if there are different points of view, please line up downstairs to spit groove
124 Reads