1. Basic elements
1.1. Identifier
Identifiers are names used in programming. They are used to name variables, constants, functions, types, interfaces, package names, etc., to establish a connection between name and use.
Identifier naming rules:
- Can only be composed of non-empty letters (Unicode), numbers, and underscores (_)
- Can only start with a letter or underscore
- Cannot use Go language keywords
- Avoid using Go language predefined identifiers
- Identifiers are case sensitive
- Hump style is recommended
Go language provides some predefined identifiers to represent built-in constants, types, and functions.
Avoid using when customizing identifiers:
1. Built-in constants: true, false, nil, iota
2. Built-in types: bool, byte, rune, int, int8, int16, int32, int64, uint, uint8, unit16,
unit32、unit64、uintptr、float32、float64、complex64、complex128、string、error
3. Built-in functions: make, len, cap, new, append, copy, close, delete, complex, real,
imag、panic、recover
4. Blank identifier: _
1.2, keywords
Keywords are used for specific grammatical structures
Go language defines 25 keywords:
- Declaration: import, package
- Entity declaration and definition: char, const, func, interface, map, struct, type, var
- Process control: break, case, continue, default, defer, else, fallthrough,
for、go、goto、if、range、return、select、switch
1.3, literal
Literals are a way of expressing values, and are often used to initialize variables/constants.
mainly divided:
- Identifies the literal value of the underlying data type, for example: 1, 1.1, true, 3 + 4i,'a','A', "I love China"
- Construct a custom compound data type type literal, for example: type Interval int
- 用于表示符合数据类型值的复合字面量,用来构造array、slice、map、struct
的值,例如:{1, 2, 3}
1.4、操作符
- 算术运算符:+、-、*、/、%、++、--
- 关系运算符:>、>=、<、<=、==、!=
- 逻辑运算符:&&、||、!
- 位运算符:&、|、^、<<、>>、&^
- 赋值运算符:=、+=、-=、*=、/=、%=、&=、|=、^=、<<=、>>=
- 其他运算符:&(单目)、*(单目)、.(点)、-(单目)、…、<-
1.5、分隔符
- 小括号(), 中括号[],大括号{},分号;,逗号,
二、基础知识点
2.1、声明
声明语句用于定义程序的各种实体对象,主要有:
- 声明变量的var
- 声明常量的const
- 声明函数的func
- 声明类型的type
2.2、变量
变量是指对一块存储空间定义名称,通过名称对存储空间的内容进行访问或修改,使用var进行变量声明,常用的语法为:
1. var 变量名 变量类型 = 值
定义变量并进行初始化
var name string = "alex"
2. var 变量名 变量类型
定义变量使用零值进行初始化
var age int
3. var 变量名 = 值
定义变量,变量类型通过值类型进行推导
var num = 11
4. var 变量名1, 变量名2 , …, 变量名n 变量类型
定义多个相同类型的变量并使用零值进行初始化
var prefix, suffix string
5. var变量名1, 变量名2 , …, 变量名n 变量类型 = 值1, 值2, …, 值n
定义多个相同类型的变量并使用对应的值进行初始化,
var prev, next int = 3, 4
6. var变量名1, 变量名2 , …, 变量名n = 值1, 值2, …, 值n
定义多个变量并使用对应的值进行初始化,变量的类型使用值类型进行推导,类型可不相同
var name, age = "alex", 25
7. 批量定义
var (
变量名1 变量类型1 = 值1
变量名2 变量类型2 = 值2
)
定义多个变量并进行初始化,批量复制中变量类型
可省略。例如:
var (
name string = "alex"
age int = 25
)
8.简短声明
a4 := "sheldon"
2.3、赋值
可以通过赋值运算=更新变量的值,Go语言支持通过元组赋值同时更新多个变量的值。
n1, n2 = 1, 2,可用于两个变量的交换x, y =
y, x
var (
n1 int = 1
n2 int = 2
)
n1,n2 = n2,n1
fmt.Println(n1,n2)
2.4、常量
常量用于定义不可被修改的的值,需要在编译过程中进行计算,只能为基础的数据类型布尔、数值、字符串,使用const进行常量声明,常用语法:
1. const 常量名 类型 = 值
定义常量并进行初始化
const pi float64 = 3.1415926
2. const 常量名 = 值
定义常量,类型通过值类型进行推导
const e = 2.7182818
3. 批量定义
const (
常量名1类型1 = 值1
常量名2类型2 = 值2
)
定义多个变量并进行初始化,批量复制中变量类型可省略,并且除了第一个常量值外其他常量可同时省略类型和值,表示使用前一个常量的初始化表达式。
const (
name string = "alex"
age int = 25
)
const (
name string = "alex"
desc
)
常量之间的运算,类型转换,以及对常量调用函数len、cap、real、imag、complex、unsafe.Sizeof得到的结果依然为常量
2.5、枚举
go中的枚举通过const+iota实现
const (
a1 int = iota
a2
a3
)
fmt.Println(a1,a2,a3)
0 1 2
2.6、作用域
作用域指变量可以使用范围。go语言使用大括号显示的标识作用域范围,大括号内包含一连串的语句,叫做语句块。语句块可以嵌套,语句块内定义的变量不能在语句块外使用
常见隐式语句块:
• 全语句块
• 包语句块
• 文件语句块
• if、switch、for、select、case语句块
作用域内定义变量只能被声明一次且变量必须使用,否则编译错误。在不同作用域可定义相同的变量,此时局部将覆盖全局。
outer := 1
{
inter :=2
fmt.Println(outer,inter)
outer = 10
{
inter1 :=3
fmt.Println(outer,inter,inter1)
}
}
2.7、注释
Go支持两种注释方式,行注释和块注释:
• 行注释:以//开头,
//我是行注释
• 块注释:以/*开头,以*/结尾,
/*我是块
注释*/
来自吴科老师《手撕go语言》,go运维开发班四期