🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] > [go-awesome]([https://shockerli.net/post/go-awesome/#%E5%AD%A6%E4%B9%A0%E9%A1%B9%E7%9B%AE](https://shockerli.net/post/go-awesome/#%E5%AD%A6%E4%B9%A0%E9%A1%B9%E7%9B%AE)) ## 安装配置 ### 类linux 配置 go 有4个环境变量需要设置: GOROOT、 GOPATH、 GOBIN,以及PATH 需要设置到某一个 profile文件中(`~/bash_profile`或`/etc/profile`) ``` > tar -zxvf go1.18.3.linux-amd64.tar.gz > mv go /usr/local/ > vim /etc/profile export GOROOT=/usr/local/go export GOBIN=$GOROOT/bin export PATH=$PATH:$GOBIN ``` 添加到对应的`profile` 配置文件中 eg: `source .zshrc` 或`source .bashrc` ### mac 1. 安装 `brew install go` 2. 如果 goland 编辑器 无法调试 `https://developer.apple.com/download/more/` 下载 `Command Line tools(macoS 10.13) for Xcode 9.1` ## 编译安装 ``` wget https://storage.googleapis.com/golang/go<VERSION>.src.tar.gz tar -zxvf go<VERSION>.src.tar.gz sudo mv go $GOROOT cd $GOROOT/src ./all.bash ``` > 完全不想运行包的测试,你可以直接运行 ./make.bash 来进行单纯的构建过程 **注意事项** q:` ‘make[2]: Leaving directory '/localusr/go/src/pkg/net’` a:暂时关闭防火墙 or ` export DISABLE_NET_TESTS=1` ## golang 插件 ### golangci-lint 语法检查工具,比 gometalinter 快五倍,网址[github.com/golangci/golangci-lin](https://github.com/golangci/golangci-lint) 各个平台[下载](https://github.com/golangci/golangci-lint/releases) ## 制作开源模块需要注意到事情 ``` export GO111MODULE=on mkdir -p src/github.com/idcpj/red_packet cd src/github.com/idcpj/red_packet go mod init github.com/idcpj/red_packet > tree ├── README.md ├── app.go ├── brun │   ├── config.ini │   └── main.go ├── go.mod ├── go.sum ├── infra │   ├── base │   │   ├── dbx.go │   │   ├── log.go │   │   └── props.go │   ├── boot.go ``` 执行自己的模块时候,需要在 `brun/main.go` 中初始化整个模块(存在 `ini()` 函数) main.go ``` _ "github.com/idcpj/red_packet" ``` 在 go.mod 中 添加(编译时,总是 编辑线上 githhub.com 的版本) ``` module github.com/idcpj/red_packet // indirect ``` 执行 ``` cd brun go run main.go ``` ### 版本控制 自定义模块,在 release 中 添加 版本 `v0.0.1` 即可 ## 下载被墙的包 1. 方法一 如需官方包下载`golang.org/x/time/rate` ``` cd $GOPATH/src/golang.rog/x/ git clone http://github.com/golang/time.git time ``` 下载第三方包 ``` cd $GOPATH/src/github.com/go-sql-driver/ git clone https://github.com/go-sql-driver/mysql 如果有些需要执行命令 如goimports go install golang.org/x/tools/cmd/goimports ``` 2. 方法二 goproxy 国内源加速 ``` export GO111MODULE=on //必须有 mod export GOPROXY=https://goproxy.cn ``` 3. 方法三:Go version >= 1.13 `go env -w GOPROXY=https://goproxy.cn` 4. 方法四: 用 mod 的 replace 代替 ``` replace ( golang.org/x/sys => github.com/golang/sys latest ) ``` ## 确定GOARCH 参数 执行`arch` or `lscpu` ``` amd64 架构 : X86_64 arm64 架构 : aarch64 mipsle 架构(通过 lscpu 查看): Architecture: mips64 Byte Order: Little Endian //小端序 mips 架构(通过 lscpu 查看): Architecture: mips64 Byte Order: Big Endian //小端序 支持的所有平台是: GOOS:android,darwin,ios,linux,windows GOARCH: ,386,amd64,arm-5,arm-6,arm-7,arm64,mips,,mipslemips64mips64le ``` ## 在不同平台编译指定文件 unix 下编译 ``` CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build main.go CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build main.go CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build main.go ``` Windows 下编译 ``` //mac SET CGO_ENABLED=0 SET GOOS=darwin SET GOARCH=amd64 go build main.go //linux SET CGO_ENABLED=0 SET GOOS=linux SET GOARCH=amd64 go build main.go ``` ``` GOOS:目标平台的操作系统(darwin、freebsd、linux、windows) GOARCH:目标平台的体系架构(386、amd64、arm) ``` ## 错误 ### 出现undefined 错误 设置编辑器用 包的形式编译 或者 指定需要的包 `go run main.go hub.go `