Go语言使用Extended Backus-Naur Form (巴克斯范式EBNF)定义编程语言的语法规则。
源代码文本使用UTF-8编码,区分大小写。
代码注释
Go 语言代码注释有两种方法:
行末注释 //这是注释
跨行注释
1 2 3
/* 这是 跨行注释*/
标识符
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
'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 12
`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
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 是不同的类型,尽管他们的字节数是一样的。 字符串类型:
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