此教程是为初学 Go 语言的小伙伴准备的简明版本的教程。
This docs is for the beginner who start to learn Go Language.
教程会有中英文的翻译。
安装 Go 语言环境。
Install Go click here
个人来说建议使用 VS Code 开发 go 程序。
Personaly I recommend the VS Code as your editor.
vs code plugin: https://marketplace.visualstudio.com/items?itemName=ms-vscode.Go
Read more here
第一个 Go 程序。
Your first Go program.
mkdir workspace && cd workspace
touch index.go
用vscode
打开workspace
文件夹。
Open the workspace
by vscode
.
添加index.go
文件,并添加以下代码:
Add the following code to index.go
package main
import "fmt"
func main() {
fmt.Println("Hello, world, aicoder.com")
}
编译上面的程序,并运行。
Build and run the code.
cd workspace
go build index.go
./index
Go 命令行工具,除了编译之外,还有格式化、测试等一系列工具。 文档地址:这里 Go command tool has several commands, e.g., doc, build, fix, fmt. Read more here
在控制台中获得如下输出:
We can get the output as below:
Hello, world, aicoder.com
|
: alternation(或)()
: grouping(分组)[]
: option (0 or 1 times)(出现 0 次或者 1 次){}
: repetition (0 to n times)(出现次数范围,出现 0 次到 n 次)
Comments serve as program documentation. There are two forms:
注释是写给程序员来阅读代码的,不会编译到最终程序中。Go 语言有两种注释(类似 C、Java、C#、JavaScript)
- Line comments start with the character sequence
//
and stop at the end of the line. - General comments start with the character sequence
/*
and stop with the first subsequent character sequence*/
.
i:= 3 // Line comments , 行注释
/*
General comments, mutiple lines comments
多行注释
*/
标识符用于声明函数、接口、类型、变量等的名字。
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
- Types:
bool byte complex64 complex128 error float32 float64
int int8 int16 int32 int64 rune string
uint uint8 uint16 uint32 uint64 uintptr
- Constants:
true false iota
- Zero value:
nil
- Functions:
append cap close complex copy delete imag len
make new panic print println real recover
+ & += &= && == != ( )
- | -= |= || < <= [ ]
* ^ *= ^= <- > >= { }
/ << /= <<= ++ = := , ;
% >> %= >>= -- ! ... . :
&^ &^=