基础语法¶
Go 程序可以由多个标记组成,可以是关键字,标识符,常量,字符串,符号。以下 GO 语句由 6 个标记组成:
fmt.Println("Hello, World!")
关键字¶
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
36 个预定义标识符:
append bool byte cap close complex complex64 complex128 uint16
copy false float32 float64 imag int int8 int16 uint32
int32 int64 iota len make new nil panic uint64
print println real recover string true uint uint8 uintptr
程序一般结构¶
// 当前程序的包名
package main
// 导入其他包
import . "fmt"
// 常量定义
const PI = 3.14
// 全局变量的声明和赋值
var name = "gopher"
// 一般类型声明
type newType int
// 结构的声明
type gopher struct{}
// 接口的声明
type golang interface{}
// 由main函数作为程序入口点启动
func main() {
Println("Hello World!")
}