配置好Go语言环境后,现在来学习Go语言的基础语法。

符号和源代码

Go语言使用Extended Backus-Naur Form (巴克斯范式EBNF)定义编程语言的语法规则。

源代码文本使用UTF-8编码,区分大小写。

代码注释

Go 语言代码注释有两种方法:

  1. 行末注释 //这是注释
  2. 跨行注释
1
2
/* 这是
跨行注释*/

标识符

Go 语言标识符由letters和digits组成,且必须由letter开头。例如:

1
2
3
4
a
_x9
ThisVariableIsExported
αβ

有些标识符已经被预先声明使用了。

关键字

以下为Go 语言的关键字,不能作为标识符:

1
2
3
4
5
break        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
2
3
4
5
6
+    &     +=    &=     &&    ==    !=    (    )
- | -= |= || < <= [ ]
* ^ *= ^= <- > >= { }
/ << /= <<= ++ = := , ;
% >> %= >>= -- ! ... . :
&^ &^=

整数字面量

1
2
3
4
5
6
7
8
int_lit     = decimal_lit | octal_lit | hex_lit .
decimal_lit = ( "1" … "9" ) { decimal_digit } .
octal_lit = "0" { octal_digit } .
hex_lit = "0" ( "x" | "X" ) hex_digit { hex_digit } .

422385796767204230 //十进制
0600 //八进制
0xabcdef //十六进制

浮点数字面量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
float_lit = decimals "." [ decimals ] [ exponent ] |
decimals exponent |
"." decimals [ exponent ] .
decimals = decimal_digit { decimal_digit } .
exponent = ( "e" | "E" ) [ "+" | "-" ] decimals .

0.
72.40
072.40 // == 72.40
2.71828
1.e+0
6.67428e-11
1E6
.25
.12345E+5

虚数字面量

1
2
3
4
5
6
7
8
9
10
imaginary_lit = (decimals | float_lit) "i" .
0i
011i // == 11i
0.i
2.71828i
1.e+0i
6.67428e-11i
1E6i
.25i
.12345E+5i

字符及字符串

字符

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
5
const Pi = 3.14159265
const(
Big = 1<<100
Small = Big>>99
)

变量:

1
2
3
4
var x interface{}  // x 值为nil,为静态类型 interface{}
var v *T // v 值为nil, 为静态 *T
x = 42 // x 值为42,动态类型 int
x = v // x 值为 (*T)(nil) ,动态类型 type *T

类型:
布尔型:

1
2
bool = true
isNice = false

数值类型:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
uint8       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
2
make([]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
8
func()
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
3
map[string]int
map[*T]struct{ x, y float64 }
map[string]interface{}

channel类型:

1
2
3
4
5
6
7
8
9
10
chan 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
8
const 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