您的位置 首页 golang

大白话 golang 教程-18-工程和包管理系统

在教程一开始就介绍了 go mod 命令,它用于管理项目的依赖项,在当前目录输入 “go mod init 模块名称” 初始化 go.mod 文件,比如想用 github.com/fatih/color 包,在项目根目录下输入:

 go get github.com/fatih/color
// 输出
go: downloading github.com/fatih/color v1.10.0
go: github.com/fatih/color upgrade => v1.10.0
go: downloading github.com/mattn/go-colorable v0.1.8
go: downloading golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae  

此时项目根目录下多了一个 go.sum 文件,记录了 color 包以及它本身的依赖,并锁定了相关的版本,为了同一个项目的开发人员有用一致的依赖和版本,该文件通常需要加入到 git 版本仓库中。

下载了依赖项,在项目里可作为模块来调用了。

 import "github.com/fatih/color"

func main() { 
  color.Cyan("我是 cyan")
  color.Red("我是 red")
}  

可以通过执行 go list -m all 或者 go mod graph 来查看当前项目的依赖项。输入 go help mod 查看 mod 相关的命令:

 download    download modules to local cache
edit        edit go.mod from tools or scripts
graph       print module requirement graph
init        initialize new module in current directory
tidy        add missing and remove unused modules
vendor      make vendored copy of dependencies
verify      verify dependencies have expected content
why         explain why packages or modules are needed  

比如 go mod vendor 命令,可以在当前目录新建一个目录 vendor,里面是一份项目依赖模块的源代码拷贝(通常该目录不要加入 git 版本库,在 .gitignore 中设置忽略,注释生成 vendor 后模块调用会优先找到 vendor 下的代码进行编译)。

如何发布一个模块到 github 给别人使用呢?

1. 使用 go mod init github.com/你的github账号名/你的模块名

2. 实现模块和编写测试代码

3. 编写文档 README.MD 以及相关的脚本

4. 在 github.com 新建远端仓库

5. 给你的项目打上 tag 标签推送至远端仓库

6. 在其他项目里执行 go get github.com/你的github账号名/你的模块名 导入使用

写一个密码生成器的 module 试试,新建目录 password-generator,执行 git mod init github.com/developdeveloper/password-generator 后,指定如下目录:

 ├── cmd
│   ├── char
│   │   └── main.go
│   └── digit
│       └── main.go
├── go.mod
├── internal
│   ├── randnum.go
│   └── randnum_test.go
├── pkg
│   ├── pwdgen.go
│   └── pwdgen_test.go  

cmd 是生成可执行文件的主程序,internal 目录是模块内部使用的代码,pkg 是模块暴露到外部的代码(通常 pkg 下应该还要建立包,由于代码太简单就省略了),生成密码的函数需要产生随机值,internal 目录实现了 RandNumber 函数:

 func RandNumber(max int) int {
  rand.Seed(time.Now().UnixNano())
  return rand.Int() % max
}  

定义的密码生成器结构体:

 func New() *PasswordGenerator {
  return &PasswordGenerator{}
}  

生成数字密码:

 func (pg *PasswordGenerator) DigitsOnly(length int) string {
  var buf bytes.Buffer
  digits := "0123456789"

  for i := 0; i < length; i++ {
    buf.WriteByte(digits[internal.RandNumber(10)])
  }

  return buf.String()
}  

生成字符密码:

 func (pg *PasswordGenerator) CharsOnly(length int) string {
  var buf bytes.Buffer
  chars := "abcdefghijklmnopkrstuvwxyz"

  for i := 0; i < length; i++ {
    buf.WriteByte(chars[internal.RandNumber(26)])
  }

  return buf.String()
}  

执行 git 命令:

 git add .
git ci -m 'initial'
git tag v1.0
git remote add origin git@github.com:developdeveloper/password-generator.git
git push -u origin master
git push --tags  

访问 ,我们写的密码生成器模块已经上传到 github 了,其他项目可以安装使用了,进入本章节的项目目录,执行 go get github.com/devellopdeveloer/password-generator 创建依赖和下载包:

 go get github.com/developdeveloper/password-generator
go: downloading github.com/developdeveloper/password-generator v0.0.0-20210111081505-6237ddb53d37
go: github.com/developdeveloper/password-generator upgrade => v0.0.0-20210111081505-6237ddb53d3  

修改 main.go 代码:

 import (
  passwordGenerator "github.com/developdeveloper/password-generator/pkg" // 包的别名
  "github.com/fatih/color"
)

func main() {
  //color.Cyan("我是 cyan")
  //color.Red("我是 red")
  pwdGen := passwordGenerator.New()
  color.Green(pwdGen.DigitsOnly(6))
  color.Blue(pwdGen.CharsOnly(6))
}  

恩,我们写的 module 可以工作了,go 语言本身并没有对工程的目录做出限定,不过通常会使用以下的目录结构:

 cmd 可执行文件,可能有多个 main 文件
internal 内部的代码,私有类型和函数
pkg 公开的类型和函数
configs 加载配置
deploy 部署相关的定义
githooks git的钩子
scripts bash脚本
init 初始化函数
vendor 依赖的拷贝
build 持续集成相关的
examples 说明的示例程序
test 测试文件
testdata 测试数据
web 网页相关的模板等
api 开放的 api 接口
docs 文档
assets 图片等资源
README.md 项目说明文件
third_party 外部辅助工具
tools 项目的支持工具
Makefile 脚本运行的入口
bin 编译好的二进制文件
LICENSE.md 版本申明文件  

要特别的提示一下,用过 Java、Node 等编程语言的同学,通常项目里会有 src 这个目录,go 项目里不建议这样做。

本章节的代码

1.

2.

文章来源:智云一二三科技

文章标题:大白话 golang 教程-18-工程和包管理系统

文章地址:https://www.zhihuclub.com/97522.shtml

关于作者: 智云科技

热门文章

网站地图