go-doc-介绍和搭建

介绍

golang官网
golang中文网
go中文api

环境搭建

windows

  1. 下载对应版本的zip
    • go1.9.2.windows-amd64.zip
  2. 解压zip到想要的目录
  3. 配置环境变量
    • GOROOT:指定SDK的安装路径D:\application\go
    • Path:添加SDK的%GOROOT%\bin目录
    • GOPATH:工作目录D:\project-go,将来我们的go项目的工作目录
  4. 测试
    • 打开cmd输入go version

linux

  1. 下载对应版本的tar.gz
  2. 解压
    • tar -zxvf go1.9.2.linux-amd64.tar.gz
  3. 配置环境变量
    • vim /etc/profile
    • 添加 export GOROOT=/opt/go
    • 添加 export PATH=$PATH:$GOROOT/bin
    • 添加 export GOPATH=$HOME/goprojects/
  4. 测试
    • 重登录用户
    • go version

mac

  1. 下载对应版本的tar.gz
    • go1.9.2.darwin-amd64.tar.gz
  2. 解压
    • tar -zxvf go1.9.2.darwin-amd64.tar.gz
  3. 配置环境变量
    • vi /etc/profile
    • 添加 export GOROOT=$HOME/go_dev/go
    • 添加 export PATH=$PATH:$GOROOT/bin
    • 添加 export GOPATH=$HOME/goprojects/

如果修改 /etc/prifile 失败,可以修改 ~/.bash_profile

export GOROOT=/Users/popolin/sdk/go1.17.5
export PATH=$PATH:$GOROOT/bin
export GOPATH=/Users/popolin/files/git/gopath

  1. 测试
    • 重登录用户
    • go version

Golang中国

注意

  • 多个gopath之间用;分割

命令

go build

编译go文件为可执行文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
-a
完全编译,不理会-i产生的.a文件(文件会比不带-a的编译出来要大?)
-n
仅打印输出build需要的命令,不执行build动作(少用)。
-p n
开多少核cpu来并行编译,默认为本机CPU核数(少用)。
-race
同时检测数据竞争状态,只支持 linux/amd64, freebsd/amd64, darwin/amd64 和 windows/amd64.
-msan
启用与内存消毒器的互操作。仅支持linux / amd64,并且只用Clang / LLVM作为主机C编译器(少用)。
-v
打印出被编译的包名(少用).
-work
打印临时工作目录的名称,并在退出时不删除它(少用)。
-x
同时打印输出执行的命令名(-n)(少用).
-asmflags 'flag list'
传递每个go工具asm调用的参数(少用)
-buildmode mode
编译模式(少用)
'go help buildmode'
-compiler name
使用的编译器 == runtime.Compiler
(gccgo or gc)(少用).
-gccgoflags 'arg list'
gccgo 编译/链接器参数(少用)
-gcflags 'arg list'
垃圾回收参数(少用).
-installsuffix suffix
a suffix to use in the name of the package installation directory,
in order to keep output separate from default builds.
If using the -race flag, the install suffix is automatically set to race
or, if set explicitly, has _race appended to it. Likewise for the -msan
flag. Using a -buildmode option that requires non-default compile flags
has a similar effect.
-ldflags 'flag list'
'-s -w': 压缩编译后的体积
-s: 去掉符号表
-w: 去掉调试信息,不能gdb调试了
-linkshared
链接到以前使用创建的共享库
-buildmode=shared.
-pkgdir dir
从指定位置,而不是通常的位置安装和加载所有软件包。例如,当使用非标准配置构建时,使用-pkgdir将生成的包保留在单独的位置。
-tags 'tag list'
构建出带tag的版本.
-toolexec 'cmd args'
a program to use to invoke toolchain programs like vet and asm.
For example, instead of running asm, the go command will run
1
2
3
4
5
go build hello.go
go build -o a.exe hello.go

# 输出数据竞争信息
go build -race hello.go

go run

运行go文件

1
go run hello.go

gofmt

格式化文件

1
2
# 格式化main.go文件
gofmt -w main.go

go mod

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 初始化模块
go mod init <模块名称>
例如:go mod init eht/frame

# 添加依赖
go get 依赖路径
例如:go get github.com/gin-gonic/gin

# 更新依赖
go get -u

# 查看依赖
go list -m all

# 下载go.mod 文件的依赖,侧重下载
go mod download

# 整理和更新go.mod 文件的依赖,侧重维护,会删掉没有使用的依赖
go mod tidy

编译

window

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# window
set GOPATH
set GOPATH=D:\go\goPath;D:\go\okFrame_v4;D:\go\okPlugins_v4;D:\feima\sanguo\server\project;
go build -tags="debug" -o window_chat.exe ./src/serverChat/main.go

# linux
set GOPATH
set GOPATH=D:\go\goPath;D:\go\okFrame_v4;D:\go\okPlugins_v4;D:\feima\sanguo\server\project;
set GOARCH=amd64
set GOOS=linux

go build -tags="debug" -o linux_sanguo_logic ./src/serverLogic/main.go

# mac
SET CGO_ENABLED=0
SET GOOS=darwin
SET GOARCH=amd64
go build main.go

mac

1
2
3
4
5
6
7
8
9
10
11
# linux
export CGO_ENABLED=0
export GOOS=linux
export GOARCH=amd64
go build main.go

# window
export CGO_ENABLED=0
export GOOS=windows
export GOARCH=amd64
go build main.go

linux

1
2
3
4
5
6
7
8
9
10
11
# mac
export CGO_ENABLED=0
export GOOS=darwin
export GOARCH=amd64
go build main.go

# window
export CGO_ENABLED=0
export GOOS=windows
export GOARCH=amd64
go build main.go

代理设置

1
2
3
4
5
# 关闭代理
go env -w GOSUMDB=off

# 设置代理
go env -w GOPROXY=https://goproxy.cn,direct

错误

gcc

runtime/cgo
exec: “gcc”: executable file not found in %PATH%

  • 解决方法

http://www.mingw-w64.org/doku.php/download
下载 MingW-W64-builds 和 Msys2

安装不成功,找已安装的人把安装目录压缩然后配置bin目录到path