关于go:go-mod-封装公司私有包并导入使用

4次阅读

共计 713 个字符,预计需要花费 2 分钟才能阅读完成。

介绍

之前写过怎么封装私有包(go 如何通过 module 新建公共包,并导入应用
),发现事实很多人,想理解封装公有包,所以再写一篇如何封装公有包,具体介绍下。

一、创立一个 Go Modules 我的项目

1、创立文件夹

mkdir tool && cd tool

2、初始化包 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、拉取包到本地

➜ go get codeup.aliyun.com/hisheng/tool

2、在代码里导入

import (
    "codeup.aliyun.com/hisheng/tool"
    "testing"
)

func TestName(t *testing.T) {t.Log(tool.Hello())
}

输入:

hello from tool project

谢谢您的观看,欢送关注我的公众号。

正文完
 0