CentOS下提升Golang编译速度的可落地方案
一 编译与缓存优化
go build -p $(nproc)。并行度过低会串行化依赖,过高会引发内存与调度开销,按核心数设置通常最优。export GOCACHE=/tmp/go-cache;必要时清理:go clean -cache。go build -ldflags "-s -w"。export GOPROXY=https://goproxy.cn。CGO_ENABLED=0 go build -ldflags "-s -w"。二 依赖与代码结构优化
go mod vendor将依赖落地,避免每次构建重复解析与下载。三 系统与硬件优化
GOCACHE与项目目录置于SSD,可显著降低I/O等待时间。/etc/security/limits.conf 增加 * soft nofile 65536 与 * hard nofile 65536,并 ulimit -n 65535。net.core.somaxconn、net.ipv4.tcp_max_syn_backlog、net.ipv4.ip_local_port_range、net.ipv4.tcp_tw_reuse、net.ipv4.tcp_fin_timeout 等可按需调优并 sysctl -p 生效。四 常用命令与配置示例
export GOCACHE=/tmp/go-cacheexport GOPROXY=https://goproxy.cngo build -p $(nproc) -ldflags "-s -w"CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-s -w" -o appCGO_ENABLED=0 GOOS=linux GOARCH=arm go build -ldflags "-s -w" -o app_armupx --best --lzma app五 定位与持续优化