! [] (Https://raw.githubusercontent.com/studygolang/gctt-images/master/simple-statement/1_HO_ NLFJ1LDRODQSO68ACWA.JPEG) [Https://en.wikipedia.org/wiki/Kiss_ (band)] (https://en.wikipedia.org/wiki/Kiss_% 28band%29) has a term in the Golang [specification] (HTTPS://GOLANG.ORG/REF/SPEC): Simple statements (simpler statement), which may not be used frequently throughout the document. But the syntax of the language only allows these statements to be used in several important places. The purpose of this essay is to introduce these simple statements and the places where they can be used. [] (https://raw.githubusercontent.com/studygolang/gctt-images/master/simple-statement/1_ Sh2ptmhloytnwptexj4fra.jpeg) <center>photography:ursula coyote/netflix</center> in the specification, it consists of SimpleStmt Production definition (using [EBNF] (https://golang.org/ref/spec#Notation)): ' Simplestmt = emptystmt | expressionstmt | sendstmt | incdecstmt | Assignment | Shortvardecl. "Below we will introduce 6 simple statements # # 1. Blank statement Obviously, in a simple statement, a blank statement is nothing. # # 2. Self-increment or decrement statement ' ' ' gox++//semantically equivalent to assignment x + = 1x--//X-= 1 ' in go, after the expression operand with "--" or "+ +" is not an expression (the go "-" or "+ +" is not an expression, but a statement), so You will not see a combination of the following tangled in the '. Go ' source file: ' ' Gofoo = bar++ + 10 ' Although this is a statement, it needs to be used in meaningful places as a simple statement and will not introduce unnecessary burdens in places where it should not be ( By making the code difficult to read and difficult to maintain. > ** There is no prefix version in Golang, i.e. "--" or "+ +" is placed before the object is manipulated. **## 3. The Send statement lexical token "<-" means that a value is sent over the channel. It does not return any values, so it does not need to be expressed. # # 4. Expression statements can be placed in statements. > * * Blocks are a series of statements enclosed in curly braces, for example, an expression statement is fully valid in the following cases. * * The following options are allowed:-function calls (except for some built-in functions, such as Append,cap,complex,imag,len,make,new,real,[unsafe] (https://golang.org/pkg/unsafe/). Alignof,unsafe. Offsetof and unsafe. SIZEOF)-Method call-Receive Operator ' Gofunc (s s) m () {FMT. Printf ("Hi%s\n", S.name)}func f (n int) {FMT. PRINTF ("F says%d\n", N)}func main () {f (1) s: = s{"Michał"} S.M () c: = make (chan int) go func () {C <-1} () <-c nu Mbers: = []int{1, 2, 3} len (numbers)//Error:len (numbers) was executed but not used} ' # # 5. Assignment statements everyone should be familiar with some of the most basic assignment forms. The variable must first be declared, and the expression on the right must be assignable (https://studygolang.com/articles/12381) to a variable: "' govar population int64population = 8000000 Assignment var city, country string//Two single-value expressions assigned to two variable city, country = "New York", "USA" when assigning more than one value to a variable list, there are two forms. In the first form, an expression returns multiple values, such as a function call: "' Gof: = Func () (Int64, String) {return 2000000," Paris "}var population int64var City Stringpopu Lation, City = f () "The second form of the right containsMany expressions, but each one is single-valued: ' ' gopopulation, city = 13000000, ' Tokyo ', ' the two forms cannot be confused: ' ' Gof: = Func () (Int64, String) {return 4000000, "Sydney"}var population int64var City, Country stringpopulation, city, country = f (), "Australia" "above this line of code will be thrown at compile time" mult Iple-value f () in single-value context ". An assignment operation is a construct consisting of a "=" sign, preceded by a two-tuple operator:-Plus (+)-minus (-)-bitwise operation OR (|) -Bitwise operation XOR (^)-multiply (*)-Divide (/)-take remainder (%)-Bitwise operation Shift Right (<< and >>)-bitwise operation and (&)-bit Clear (&^) ' a op= B ' (e.g. A + = b) semantically equivalent To ' A = a op b ', but a is computed only once. The assignment operation is also a valid assignment statement. # # 6. Simple variable declarations below are abbreviations for regular variable statements that need to be initialized. In addition, a type that is not explicitly specified is inferred from the type used in the initialization statement: ' ' Gos: = s{}a: = [...] Int{1,2,3}one, both: = f (), G () "" Now we should know exactly what constitutes a valid set of simple statements. But where do they use it? # # If statement simple statements can be selectively placed before the conditional expression. It is often used to declare a variable (using a short variable statement) and then use it in an expression: "' GoGen: = Func () int8 {return 10}if num: = Gen (); num > 5 {fmt. Printf ("%d is greater than 5\n", num)} "# # for statements in the Initialize or declare variables in the For statement can only be simple statements. A common use is to declare a short variable as an initial declaration and an assignment operation or a self-increment/decrement statement: "' gofor I: = 0; I < 10; i + = 1 {fmt. Println (i)} "of course, there is nothing to prevent programmers from using other simple statements in this place. # # switch statement similar to the IF statement, you can put an optional simple statement in the expression before the [switch expression] (https://golang.org/ref/spec#Expression_switches): ' goswitch aux: = 2; Letter {case ' a ': FMT. Println (aux) case ' B ': FMT. Println (aux << 1) case ' C ': FMT. Println (aux << 2) default:fmt. Println ("no Match")} "or in front of [type switch] (https://golang.org/ref/spec#Type_switches) guard: ' ' Gotype I interface {f ( Num int8) Int8}type T1 struct{}func (T T1) f (num int8) int8 {return 1}type T2 Struct{}func (T T2) f (num int8) int8 {Retu RN 2}...var I i = t1{}switch aux: = 10; Val: = I. (type) {case nil:fmt. Printf ("Nil%T%d\n", Val, aux) Case t1:fmt. Printf ("T1%T%d\n", Val, aux) Case t2:fmt. Printf ("T2%T%d\n", Val, aux)} "its output is:" ' T1 Main. T1 10 "
via:https://medium.com/golangspec/simple-statement-notion-in-go-b8afddfc7916
Author: Michałłowicki Translator: Yousanflics 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
360 reads ∙1 likes