自学Go语言之基础语法
配置好Go语言环境后,现在来学习Go语言的基础语法。
符号和源代码
Go语言使用Extended Backus-Naur Form (巴克斯范式EBNF)定义编程语言的语法规则。
源代码文本使用UTF-8编码,区分大小写。
代码注释
Go 语言代码注释有两种方法:
- 行末注释 //这是注释
- 跨行注释
1 | /* 这是 |
标识符
Go 语言标识符由letters和digits组成,且必须由letter开头。例如:
1 | a |
有些标识符已经被预先声明使用了。
关键字
以下为Go 语言的关键字,不能作为标识符:1
2
3
4
5break default func interface select
case defer go map struct
chan else goto package switch
const fallthrough if range type
continue for import return var
操作符和分隔符
1 | + & += &= && == != ( ) |
整数字面量
1 | int_lit = decimal_lit | octal_lit | hex_lit . |
浮点数字面量
1 | float_lit = decimals "." [ decimals ] [ exponent ] | |
虚数字面量
1 | imaginary_lit = (decimals | float_lit) "i" . |
字符及字符串
字符1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17'a'
'ä'
'本'
'\t'
'\000'
'\007'
'\377'
'\x07'
'\xff'
'\u12e4'
'\U00101234'
'\'' // rune literal containing single quote character
'aa' // illegal: too many characters
'\xa' // illegal: too few hexadecimal digits
'\0' // illegal: too few octal digits
'\uDFFF' // illegal: surrogate half
'\U00110000' // illegal: invalid Unicode code point
字符串1
2
3
4
5
6
7
8
9
10
11`abc` // same as "abc"
`\n
\n` // same as "\\n\n\\n"
"\n"
"\"" // same as `"`
"Hello, world!\n"
"日本語"
"\u65e5本\U00008a9e"
"\xff\u00FF"
"\uD800" // illegal: surrogate half
"\U00110000" // illegal: invalid Unicode code point
常量,变量及类型
常量以“const”表示1
2
3
4
5const Pi = 3.14159265
const(
Big = 1<<100
Small = Big>>99
)
变量:1
2
3
4var x interface{} // x 值为nil,为静态类型 interface{}
var v *T // v 值为nil, 为静态 *T
x = 42 // x 值为42,动态类型 int
x = v // x 值为 (*T)(nil) ,动态类型 type *T
类型:
布尔型:1
2bool = true
isNice = false
数值类型:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22uint8 the set of all unsigned 8-bit integers (0 to 255)
uint16 the set of all unsigned 16-bit integers (0 to 65535)
uint32 the set of all unsigned 32-bit integers (0 to 4294967295)
uint64 the set of all unsigned 64-bit integers (0 to 18446744073709551615)
int8 the set of all signed 8-bit integers (-128 to 127)
int16 the set of all signed 16-bit integers (-32768 to 32767)
int32 the set of all signed 32-bit integers (-2147483648 to 2147483647)
int64 the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)
float32 the set of all IEEE-754 32-bit floating-point numbers
float64 the set of all IEEE-754 64-bit floating-point numbers
complex64 the set of all complex numbers with float32 real and imaginary parts
complex128 the set of all complex numbers with float64 real and imaginary parts
含有具体大小的预定义类型:
byte alias for uint8
rune alias for int32
uint either 32 or 64 bits
int same size as uint
uintptr an unsigned integer large enough to store the uninterpreted bits of a pointer value
不同类型之间可以互相进行类型转换。int32 和 int 是不同的类型,尽管他们的字节数是一样的。
字符串类型:1
2
3
4字符串为常量,一经声明,不可改变。
myWebSiteDomian = "www.tbfeng.com"
len(myWebSiteDomian) //the size of myWebSiteDomian
myWebSiteDomian[0] // 'w'
数组类型:1
2
3
4
5[32]byte
[2*N] struct { x, y int32 }
[1000]*float64
[3][5]int
[2][2][2]float64 // same as [2]([2]([2]float64))
切片类型:1
2make([]int, 50, 100)
new([100]int)[0:50]
机构体类型:1
2
3
4
5
6
7
8
9
10
11// An empty struct.
struct {}
// A struct with 6 fields.
struct {
x, y int
u float32
_ float32 // padding
A *[]int
F func()
}
指针类型:1
2*Ponit
*[4]int
函数类型:1
2
3
4
5
6
7
8func()
func(x int) int
func(a, _ int, z float32) bool
func(a, b int, z float32) (bool)
func(prefix string, values ...int)
func(a, b int, z float64, opt ...interface{}) (success bool)
func(int, int, float64) (float64, *[]int)
func(n int) func(p *T)
接口类型:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23// A simple File interface
interface {
Read(b Buffer) bool
Write(b Buffer) bool
Close()
}
type ReadWriter interface {
Read(b Buffer) bool
Write(b Buffer) bool
}
type File interface {
ReadWriter // same as adding the methods of ReadWriter
Locker // same as adding the methods of Locker
Close()
}
type LockedFile interface {
Locker
File // illegal: Lock, Unlock not unique
Lock() // illegal: Lock not unique
}
map类型:1
2
3map[string]int
map[*T]struct{ x, y float64 }
map[string]interface{}
channel类型:1
2
3
4
5
6
7
8
9
10chan T // can be used to send and receive values of type T
chan<- float64 // can only be used to send float64s
<-chan int // can only be used to receive ints
The <- operator associates with the leftmost chan possible:
chan<- chan int // same as chan<- (chan int)
chan<- <-chan int // same as chan<- (<-chan int)
<-chan <-chan int // same as <-chan (<-chan int)
chan (<-chan int)
常量类型的声明:1
2
3
4
5
6
7
8const Pi float64 = 3.14159265358979323846
const zero = 0.0 // untyped floating-point constant
const (
size int64 = 1024
eof = -1 // untyped integer constant
)
const a, b, c = 3, 4, "foo" // a = 3, b = 4, c = "foo", untyped integer and string constants
const u, v float32 = 0, 3 // u = 0.0, v = 3.0
- Blog Link: https://www.tbfeng.com/learn-go-basic-grammer.html
- Copyright Declaration: The author owns the copyright, please indicate the source reproduced.