模块开发 ******************************* 参考资料: * 发布模块: https://go.dev/doc/modules/publishing * 从私有仓库安装模块: https://go.dev/doc/faq#git_https * GOPRIVATE: https://go.dev/doc/modules/managing-dependencies#proxy_server * 第三方资料: https://www.php.cn/faq/487128.html 案例 ================================ 1. ssh方式安装私有仓库的go模块 ---------------------------------------- 假设创建私有仓库 ``https://gitee.com/luzhenxiong/go-module-demo`` .. code-block:: console go mod init gitee.com/luzhenxiong/go-module-demo 提供模块内容 .. code-block:: go package driver func Sum(a int, b int) int { return a + b } 按照文档配置ssh连接 .. image:: /_static/language/golang/module/ssh克隆仓库.png 配置环境变量GOPRIVATE .. code-block:: console export GOPRIVATE=gitee.com/luzhenxiong/go-module-demo 如果不配置,代理服务器将以https的方式访问仓库,导致无法访问。 2. 变更模块版本 ---------------------------------------- 还是 ``https://gitee.com/luzhenxiong/go-module-demo`` 这个仓库,假设开发v2版本 修改go.mod .. code-block:: text module gitee.com/luzhenxiong/go-module-demo/v2 go 1.20 推送到仓库,然后下载安装的语句如下 .. code-block:: go get -v gitee.com/luzhenxiong/go-module-demo/v2 如果打tag发布v2.0.1,那么下载安装的语句如下 .. code-block:: go get -v gitee.com/luzhenxiong/go-module-demo/v2@v2.0.1 第三方案例 ------------------------------ https://github.com/apache/tinkerpop/tree/3.7.1/gremlin-go 本地以http认证方式从私有仓库安装go模块 =========================================================== 用户故事: ci平台有token拉取私有仓库,以下将的是本地开发环境下安装私有仓库go模块 .. warning:: 没找到从本地目录安装go模块的办法,猜测是要获取git的日期时间,而本地没有这个时间 私有仓库地址: https://e.coding.net/xxxx-devops/pkgname/module-name.git 1. 配置git认证 ----------------------------------- https://go.dev/ref/mod#private-modules 编辑 :file:`$HOME/.netrc` 文件 .. code-block:: text machine e.coding.net login jrgopher password hunter2 2. 配置GOPRIVATE变量 -------------------------------------- https://go.dev/ref/mod#private-module-privacy .. code-block:: console $ export GOPRIVATE=e.coding.net 这步的作用是执行go get时,若域名跟GOPRIVATE匹配上,就会在 :file:`$HOME/.netrc` 匹配域名,匹配上了会在http请求上加上账号和密码的认证 3. 从私有仓库安装模块 --------------------------------------- .. code-block:: console $ go get e.coding.net/xxxx-devops/pkgname/module-name 默认情况下,是拉取git的最新标签号