1.3.1-install
2024-07-11 15:18:17 # 1.3-Golang

https://go.dev/doc/install

install

以Linux环境为例。

1
2
3
4
5
6
7
8
9
10
11
# downlaod in China
wget https://studygolang.com/dl/golang/go1.22.5.linux-amd64.tar.gz

# upzip
sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf go1.22.5.linux-amd64.tar.gz

# link
sudo ln -s /usr/local/go/bin/* /usr/local/bin/

# checkout
go version

proxy config

首先配置代理,方便后续的模块下载

1
2
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct

Command

common

go 的一些常用命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 运行当前目录main包内的main函数
go run .

# 模块相关
# 创建一个模块,名称为 example.com/hello
go mod init example.com/hello

# 整理当前代码中引用的包,并且自动下载指定的包,并且更新mod文件
go mod tidy

# 构建代码,并生成可执行文件
go build

# 构建代码,生成可执行文件,并且安装到 GOPATH
go install

mod

1
2
3
4
mkdir hello
go mod init example/hello

go mod tidy

创建一个新模块, 模块就是go的最小分发单位。可以后续将这个模块publish,共享给其他人使用

When you ran go mod tidy, it located and downloaded the rsc.io/quote module that contains the package you imported. By default, it downloaded the latest version – v1.5.2.

run

run 命令的作用是编译和运行,且不会产生可执行的文件,一般用于调试开发使用。

While the go run command is a useful shortcut for compiling and running a program when you’re making frequent changes, it doesn’t generate a binary executable.

test

测试命令可以运行所有的测试函数,测试函数的名称以 Test开始,且test文件必须要以 xxx_test.go 进行命名。可以添加-v 获得详细的输出

The go test command executes test functions (whose names begin with Test) in test files (whose names end with _test.go). You can add the -v flag to get verbose output that lists all of the tests and their results.

build

build 命令会生成当前项目的可执行文件