go mod 封装公司私有包,并导入使用
文章目录
更方便的在微信公众号阅读文章可以关注公众号:海生的go花园
介绍
之前写过怎么封装公有包(go如何通过module新建公共包,并导入使用
),发现现实很多人,想了解封装私有包,所以再写一篇如何封装私有包,详细介绍下。
一、创建一个 Go Modules 项目
1、创建文件夹
mkdir tool && cd tool2、初始化包 go mod init codeup.aliyun.com/hisheng/tool
➜ go mod init codeup.aliyun.com/hisheng/tool
go: creating new go.mod: module codeup.aliyun.com/hisheng/tool说明:我们把代码包发到 阿里云codeup私有git仓库上来管理。
3、添加业务代码
新建tool.go文件
➜ touch tool.go 写入代码
package tool
func Hello() string {
return "hello from tool project"
}二、发布到阿里云私有远程代码仓库
git init
git add .
git commit -m "init"
git remote add origin https://codeup.aliyun.com/hisheng/tool.git
git push -u origin master三、如何拉取导入公共包
1、在shell脚本里,配置私有仓库地址全局变量
export GOPRIVATE="codeup.aliyun.com"2、拉取包到本地
➜ go get codeup.aliyun.com/hisheng/tool3、在代码里导入
import (
"codeup.aliyun.com/hisheng/tool"
"testing"
)
func TestName(t *testing.T) {
t.Log(tool.Hello())
}输出:
hello from tool project
谢谢您的观看,欢迎关注我的公众号。

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论