|
@@ -15,15 +15,15 @@
|
|
|
```
|
|
|
GOROOT=C:\go
|
|
|
GOPATH=%USERPROFILE%\go
|
|
|
+GO111MODULE=auto
|
|
|
+GOPROXY=https://goproxy.cn
|
|
|
```
|
|
|
|
|
|
PATH环境变量末尾添加 %GOROOT%\bin
|
|
|
|
|
|
**MAC OS X**
|
|
|
|
|
|
-> cd
|
|
|
-
|
|
|
-> sudo vi .bash_profile
|
|
|
+> sudo vi ~/.bash_profile
|
|
|
|
|
|
修改以下项目
|
|
|
|
|
@@ -31,95 +31,54 @@ PATH环境变量末尾添加 %GOROOT%\bin
|
|
|
export GOROOT=/usr/local/go
|
|
|
export GOPATH=$HOME/go
|
|
|
export PATH="${GOROOT}/bin:${PATH}"
|
|
|
+export GO111MODULE=auto
|
|
|
+export GOPROXY=https://goproxy.cn
|
|
|
```
|
|
|
|
|
|
> source .bash_profile
|
|
|
|
|
|
更新配置
|
|
|
|
|
|
-
|
|
|
与Java、Python、NodeJS、PHP等均不同,只是开发环境需要安装用于代码编译,生产环境直接运行项目编译好的可执行文件即可,无需安装任何类似JRE、Python、v8、PHP的虚拟机或运行时环境!
|
|
|
|
|
|
执行以下操作前请理解并确认:
|
|
|
1. GOPATH 环境变量已配置,例如配置到用户目录下的go目录
|
|
|
2. GOPATH 目录是用于存放golang项目和其相关依赖的目录,所有golang项目代码都应该位于GOPATH目录中的src子目录下的包括代码托管地址、组织名、项目名在内的多级子目录下
|
|
|
+3. GO111MODULE 环境变量指定golang管理依赖包的模式,auto,则当项目中有go.mod文件时,采用go mod模式
|
|
|
+4. GOPROXY设置go mod获取依赖包的代理,此处设置七牛云官方提供的代理
|
|
|
|
|
|
## 获取项目
|
|
|
-
|
|
|
-### 获取
|
|
|
-
|
|
|
-> go get -v -insecure -d git.aionnect.com/aionnect/go-common
|
|
|
-
|
|
|
-参数:-v 显示详情,-insecure 非https版本仓库路径,-d 仅获取,不编译安装到GOPATH
|
|
|
|
|
|
-可能会卡住或者报错,不用管,只要项目本身源代码文件获取下来了即可,文件保存位置即在上述GOPATH目录中的子目录里
|
|
|
+### GO MOD
|
|
|
|
|
|
-**完成上述操作之后,以后日常开发都可以正常使用git命令来获取和提交此项目代码**
|
|
|
+golang已经是1.14或更新版本,强烈建议项目都采用go mod方式进行依赖包管理
|
|
|
|
|
|
-### 建立软连接
|
|
|
+go mod方式下,不再使用GOPATH src目录,自有项目也不再强制要求放置于全局或额外GOPATH的src子目录结构中,go mod版本号来源于git tag
|
|
|
|
|
|
-建立软连接的目的仅仅是为了方便找而已!
|
|
|
-
|
|
|
-使用GoLand等开发工具时,仍然打开GOPATH里面的项目目录,而不是打开软连接位置的!
|
|
|
-
|
|
|
-**Windows**
|
|
|
-
|
|
|
-Power Shell 或 Windows CMD (需要用管理员身份运行):
|
|
|
-
|
|
|
-**注意,Windows Power Shell打开后首先输入cmd进入Windows CMD,下同不再累述**
|
|
|
-
|
|
|
-> mklink /D %USERPROFILE%\Documents\project\haoqitour\go-common %GOPATH%\src\git.aionnect.com\haoqi\go-common
|
|
|
-
|
|
|
-**MAC OS X**
|
|
|
+但go install构建结果依然放在全局GOPATH的bin目录中,且go mod的带版本依赖包也放置在全局GOPATH的pkg目录中
|
|
|
|
|
|
-> sudo ln -s $GOPATH/src/git.aionnect.com/aionnect/go-common/ ~/Documents/project/haoqitour/go-common
|
|
|
+go mod项目初始化,建议mod名称为代码仓库全路径,可保持在所有项目中引用路径一致,如
|
|
|
|
|
|
-## 相关依赖
|
|
|
+> go mod init git.aionnect.com/aionnect/go-common
|
|
|
|
|
|
-### Golang官方包
|
|
|
+获取和清理依赖包
|
|
|
|
|
|
-Golang运行时 >= 1.10.3
|
|
|
-
|
|
|
-其他常用golang官方包,因为golang.org被墙,所以用以下步骤获取:
|
|
|
-
|
|
|
-**Windows**
|
|
|
-
|
|
|
-```cmd
|
|
|
-git clone https://github.com/golang/net.git %GOPATH%\src\golang.org\x\net
|
|
|
-git clone https://github.com/golang/text.git %GOPATH%\src\golang.org\x\text
|
|
|
-git clone https://github.com/golang/tools.git %GOPATH%\src\golang.org\x\tools
|
|
|
-git clone https://github.com/golang/sys.git %GOPATH%\src\golang.org\x\sys
|
|
|
-git clone https://github.com/golang/crypto.git %GOPATH%\src\golang.org\x\crypto
|
|
|
-cd %GOPATH%\src
|
|
|
-go install golang.org\x\text
|
|
|
-```
|
|
|
-
|
|
|
-**MAC OS X**
|
|
|
-
|
|
|
-```bash
|
|
|
-git clone https://github.com/golang/net.git $GOPATH/src/golang.org/x/net \
|
|
|
-&& git clone https://github.com/golang/text.git $GOPATH/src/golang.org/x/text \
|
|
|
-&& git clone https://github.com/golang/tools.git $GOPATH/src/golang.org/x/tools \
|
|
|
-&& git clone https://github.com/golang/sys.git $GOPATH/src/golang.org/x/sys \
|
|
|
-&& git clone https://github.com/golang/crypto.git $GOPATH/src/golang.org/x/crypto \
|
|
|
-&& cd $GOPATH/src/ \
|
|
|
-&& go install golang.org/x/text
|
|
|
-```
|
|
|
-
|
|
|
-### 第三方包
|
|
|
+> go mod tidy
|
|
|
+
|
|
|
+### 获取
|
|
|
|
|
|
-如果有未下载的依赖包,进入项目目录,执行以下命令即可
|
|
|
+获取非https的私有仓库,需要如下处理
|
|
|
|
|
|
-> go get -v
|
|
|
+如果使用go mod,那么先执行下列命令临时关闭go mod代理
|
|
|
|
|
|
-本项目主要依赖包如下
|
|
|
+> export GOPROXY=
|
|
|
|
|
|
-[结构化日志logrus](https://github.com/Sirupsen/logrus)
|
|
|
+获取包
|
|
|
|
|
|
-[结构化日志logrus输出到graylog](https://gopkg.in/gemnasium/logrus-graylog-hook.v2)
|
|
|
+> go get -v -insecure -d git.aionnect.com/aionnect/go-common
|
|
|
|
|
|
-[配置文件读取viper](https://github.com/spf13/viper)
|
|
|
+参数:-v 显示详情,-insecure 非https版本仓库路径,-d 仅获取,不编译安装到GOPATH
|
|
|
|
|
|
-[数据库ORM框架xorm](https://github.com/go-xorm/xorm)
|
|
|
+重启控制台,或临时设置go mod代理,使go mod代理恢复
|
|
|
|
|
|
-默认的golang依赖库是全局的,如果希望各个项目各自管理自己的依赖,可以使用 [godep](https://github.com/tools/godep)
|
|
|
+> export GOPROXY=https://goproxy.cn
|