There are five types of lexical elements in the Go language: 1. Identifiers, which are character sequences composed of a number of letters (encoded by Unicode), underscores and numbers; 2. Keywords, which are reserved by the programming language. Character sequences that are not allowed to be used by programmers as identifiers can also be called reserved words; 3. Operators are symbols used to perform specific arithmetic operations or logical operations; 4. Delimiters; 5. Literals, which are values A notation.
The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.
The language symbols of Go language are also called lexical elements, which include five categories: identifier, keyword, operator, delimiter, and Literal , they are the most basic units that make up Go language codes and programs.
In general, spaces, horizontal tabs, carriage returns, and line feeds are ignored unless they are used as part of the delimiter between multiple language symbols. There is no need to insert semicolons explicitly in the Go language. When necessary, the Go language will automatically insert semicolons into the code to separate statements.
The Go language code consists of several Unicode characters. All source codes of the Go language must be encoded in the UTF-8 encoding format of the Unicode encoding specification (that is to say, the written Go language source code file must be UTF- 8 encoding format).
The language symbols of Go language are also called lexical elements, including 5 categories: identifier (identifier), keyword (keyword), operation Character (operator), separator (delimiter), and literal (literal). In general, spaces, horizontal tabs, carriage returns, and line feeds are ignored unless they are used as part of a separator between multiple language symbols. There is no need to insert semicolons explicitly in the Go language. When necessary, the Go language will automatically insert semicolons into the code to separate statements.
The Go language code consists of several Unicode characters. All source codes of the Go language must be encoded in the UTF-8 encoding format of the Unicode encoding specification (that is to say, the written Go language source code file must be UTF- 8 encoding format).
The identifier of Go language is a character sequence composed of several letters (encoded by Unicode), underscores and numbers; this character sequence The first character of must be a letter.
Note:
In Go language code, every identifier must be declared before use.
A declaration binds a non-empty identifier to a constant, type, variable, function, or code package.
In the same code block, repeated declaration of the same identifier is not allowed (except for assignment statements).
Identifiers in a source code file and a code package need to follow this rule.
The scope of a declared identifier is the same as the scope of the code block to which it directly belongs.
Strictly speaking, the code package declaration statement is not a statement. Because the code package name does not appear in any scope. The purpose of the code package declaration statement is to identify whether several source code files belong to the same code package, or to specify the default code package reference name when importing the code package.
Qualified identifiers are used to access variables or types in other code packages. For example, when I need to access a constant named O_RDONLY in the code package os, I need to write os.O_RDONLY like this.
Qualified identifiers can be used, and two prerequisites need to be met:
The code package to be accessed must be imported in advance;
Identifiers in this code package must be exportable.
An exportable identifier also needs to meet two prerequisites:
The first character in the identifier name must be uppercase (Go language determines the access permission of this identifier based on the case of the first character in the identifier name. When the first character of the identifier name is uppercase, its access permission is "public", that is The identifier can be accessed by any code in any code package through the qualified identifier; when the first character of the identifier is lowercase, its access permission is "package-level private", that is, only the same identifier as the identifier is accessed. It can only be accessed by code in a code package);
The identifier must be the name of a variable or type declared in a code package, or belong to a structure The type's field name or method name.
Predefined identifiers of Go language:
There is an empty identifier in the Go language, which is represented by an underscore and is generally used in a statement that does not require the introduction of a new binding. For example, when we only want to execute the initialization function in a certain code package without using any program entities in this code package, we can write the following import statement:
Among them, "runtime/cgo" represents the identifier of a standard library code package.
Keywords (also called reserved words) are reserved by programming languages and are not used by programmers as identifiers The character sequence to use.
Category | Keywords | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Program Statement | import, package | ||||||||||||
chan, const, func, interface, map, struct, type, var | |||||||||||||
go, select, break, case, continue, default, defer, else, fallthrough, for, goto, if, range, return, switch |
Symbol | Description | Example | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Logic and operation. Binary, logical operator | true && false //The expression result is false | ||||||||||||
Equality judgment operation. Binary, comparison operator | "abc" == "abc" //The result is true | ||||||||||||
Unequal judgment operation. Binary, comparison operator | "abc" != "Abc" //The result is true | ||||||||||||
is less than the judgment operation. Binary, comparison operator | 1 < 2 //The expression result is true | ||||||||||||
is less than or equal to. Binary, comparison operator | 1 <= 2 //The expression result is true | ||||||||||||
is greater than the judgment operation. Binary, comparison operator | 3 > 2 //The expression result is true | ||||||||||||
is greater than or equal to. Binary, comparison operator | 3 >= 2 //The expression result is true | ||||||||||||
means summation, one yuan is two Yuan, arithmetic operator | 1 //The result is 1 (1 2) //The result is 3 | ||||||||||||
represents the difference, one yuan It is binary again, arithmetic operator | -1 //The result is -1 (1 – 2) //The result is -1 | ||||||||||||
Bitwise OR operation, binary, arithmetic operator | 5 \ 11 //The result of the expression is 15 | ||||||||||||
Press Bit XOR, one element is binary, arithmetic operator | 5 | 11//The result is 14 (5)//The result is -6 | |||||||||||
Product or value, one yuan, two binary, arithmetic, address | *p //Value operation | ||||||||||||
Quotient operation, binary, arithmetic operator | 10 / 5 //The result of the expression is 2 | ||||||||||||
Remainder operation, binary, arithmetic operator | 12 % 5 //The result of the expression is 2 | ||||||||||||
Bitwise left shift operation, binary, arithmetic operator | 4 << 2 //The result of the expression is 16 | ##>> | |||||||||||
4 >> 2 //The result of the expression is 1 | ##& | ||||||||||||
&v //Address operation | &^ | ||||||||||||
5 &^ 11 //The result of the expression is 4 | ! | ||||||||||||
!b //If b is true, the result is false | ##<- | Receive operation, Unary, receiving operator | |||||||||||
优先级 | 操作符 |
---|---|
5 | * / % << >> & &^ |
4 | + - \ ^ |
3 | == != < <= > >= |
2 | && |
1 |
(1) 使用操作数来表示;
(2) 使用类型转换来表示;
(3) 使用内建函数调用来表示;
(4) 一个基本表达式和一个选择符号组成选择表达式;
例如,如果在一个结构体类型中存在字段f,我们就可以在这个结构体类型的变量x上应用一个选择符号来访问这个字段f,即x.f。其中,.f就是一个选择符号。注意:前提是这个变量x的值不能是nil。在Go语言中,nil用来表示空值。
(5) 一个基本表达式和一个索引符号组成索引表达式;
索引符号由狭义的表达式(仅由操作符和操作数组成)和外层的方括号组成,例如[]int{1,2,3,4,5}[2]就是索引表达式。
Go语言允许如下的赋值语句:
如上a为字典类型,x为字典的键。该索引表达式的结果是一对值,而不是单一值。第一个值的类型就是该字典类型的元素类型,而第二个值则是布尔类型。与变量ok绑定的布尔值代表了在字典类型a中是否包含了以x为键的键值对。如果在a中包含这样的键值对,那么赋给变量ok的值就是true,否则就为false。
**注意:**虽然当字典类型的变量a的值为nil时,求值表达式a[x]并不会发生任何错误,但是在这种情况下对a[x]进行赋值却会引起一个运行时恐慌( Go语言异常)。
(6) 一个基本表达式和一个切片符号组成切片表达式;
切片符号由2个或3个狭义的表达式和外层的方括号组成,这些表达式之间由冒号分隔。切片符号作用与索引符号类似,只不过索引符号针对的是一个点,切片符号针对的是一个范围。例如,要取出一个切片[]int{1,2,3,4,5}的第二个到第四个元素,那么可以使用切片符号的表达式[]int{1,2,3,4,5}[1:4],该结果还是一个切片。
切片表达式a[x:y:z],a是切片符号[x:y]的操作对象。其中,x代表了切片元素下界索引,y代表了切片的元素上界索引,而z则代表了切片的容量上界索引。约束如下:
0 <= 元素下界索引 <= 元素上界索引 <= 容量上界索引 <= 操作对象的容量
设a的值为[]int{1,2,3,4,5},则切片表达式a[:3]等同于a[0:3],这是因为切片符号的元素下界索引的默认值为0,相应的元素上界的索引的默认值为操作对象的长度值或容量值,即切片表达式a[3:]等同于a[3:5]。同样,切片表达式a[:]等同于复制a所代表的值并将这个复制品作为表达式的求值结果。
注意: UTF-8 编码格式会以3个字节来表示一个中文字符,而切片操作是针对字节进行的。
如果有“Go并发编程实战”的字符串类型的变量a,那么切片表达式a[1:3]的结果不是“o并”,而a[1:5]的结果才是“o并”。
(7) 一个基本表达式和一个类型断言符号组成;
类型断言符号以一个英文句号为前缀,并后跟一个被圆括号括起来的类型名称或类型字面量。类型断言符号用于判断一个变量或常量是否为一个预期的类型,并根据判断结果采取不同的响应。例如,如果要判断一个int8类型的变量num是否是int类型,可以这样编写表达式:interface{}(num).(int)。
对于一个求值结果为接口类型值的表达式x和一个类型T,对应的类型断言为:
该表达式的作用是判断“x不为nil且存储在其中的值是T类型的”是否成立。
如果T不是一个接口类型,那么x.(T)会判断类型T是否为x的动态类型(一个变量的动态类型就是在运行期间存储在其中的值的实际类型);而这个实际类型必须是该变量声明的那个类型的一个实现类型,否则就根本不可能在该变量中存储这一类型的值。所以类型T必须为x的类型的一个实现类型,而在Go语言中只有接口类型可以被其他类型实现,所以x的求值结果必须是一个接口类型的值。
所以上面表达式interface{}(num).(int)中表达式interface{}(num)的含义就是将变量num转换为interface{}类型的值(即它的结果值是接口类型的),而这刚好符合前面的定义。
知识点: interface{}是一个特殊的接口类型,代表空接口。所有类型都是它的实现类型。
在对变量的赋值或初始化的时候,也可以使用类型断言,如下:
当使用类型断言表达式同时对两个变量进行赋值时,如果类型断言成功,那么赋给第一个变量的将会是已经被转换为T类型的表达式x的求值结果,否则赋给第一个变量的就是类型T的零值。布尔类型会被赋给变量ok,它体现了类型断言的成功(true)与否(false)。
注意: 在这种场景下,即使类型断言失败也不会引发运行时恐慌。
(8) 一个基本表达式和一个调用符号组成。
调用符号只针对于函数或者方法。与调用符号组合的基本表达式不是一个代表代码包名称(或者其别名)的标识符就是一个代表结构体类型的方法的名称的标识符。调用符号由一个英文句号为前缀和一个被圆括号括起来的参数列表组成,多个参数列表之间用逗号分隔。例如,基本表达式os.Open(“/etc/profile”)表示对代码包os中的函数Open的调用。
如果函数f可以接受的参数的数量是不固定的,那么函数f就是一个能够接受可变长参数的函数,简称可变参函数。在Go语言中,在可变参函数的参数列表的最后总会出现一个可变长参数,这个可变长参数的类型声明形如…T。Go语言会在每次调用函数f的时候创建一个切片类型值,并用它来存放这些实际函数。这个切片类型值的长度就是当前调用表达式中与可变长参数绑定的实际参数的数量。
可变参函数appendIfAbsent声明如下(函数体省略):
针对此函数的调用表达式如下:
其中,与可变参数t绑定的切片类型值为[]string{”C”,”B”,”A”},包含了实际参数”C”,”B”和”A”。
也可以直接把一个元素类型为T的切片类型值赋给…T类型的可变长参数,如下调用:
或者如果有一个元素类型为stirng的切片类型的变量s的话,如下调用:
对于将切片类型的变量赋给可变长参数的情况,Go语言不会专门创建一个切片类型值来存储其中的实际参数。因为,这样的切片类型值已经存在了,可变长参数t的值就是变量s的值。
The above is the detailed content of There are several types of lexical elements in Go language. For more information, please follow other related articles on the PHP Chinese website!