GO语言包管理工具go mod及包应用实例分析

    1.GO中包的定义与介绍

    go中包分为三种:1.系统内置包 2. 自定义包 3.第三方包

    GO语言包管理工具go mod及包应用实例分析

    2. 包管理工具 go mod

    GO语言包管理工具go mod及包应用实例分析

    2.1 自定义包 (可以包含多个go文件)

    2.1.1 建立项目并go mod init初始化

    GO语言包管理工具go mod及包应用实例分析

    GO语言包管理工具go mod及包应用实例分析

    GO语言包管理工具go mod及包应用实例分析

    2.1.2 自定义一个包(文件夹)并书写方法

    GO语言包管理工具go mod及包应用实例分析

    2.1.3 引入自定义包,并使用

    GO语言包管理工具go mod及包应用实例分析

    GO语言包管理工具go mod及包应用实例分析

    2.1.4 函数init执行顺序 init函数会自动执行

    GO语言包管理工具go mod及包应用实例分析

    3. golang中使用第三方包

    3.1 查找第三方包

    我们可以在https://pkg.go.dev查找常见的golang第三方包

    比如 解决float精度丢失的包decimal

    GO语言包管理工具go mod及包应用实例分析

    3.2 安装包

    3.2.1 go get (全局)

    受网络原因限制

    3.2.2 go download (全局)

    GO语言包管理工具go mod及包应用实例分析

    3.2.3 go mod vender(本项目)(主用)

    将依赖复制到当前项目的vendor下

    GO语言包管理工具go mod及包应用实例分析

    补充:常用命令

    1、初始化模块

    go  mod  init   模块名           

    2 、获取远程的模块

    go get  模块名           

    3、 查看当前依赖模块

    go list -m all              

     4、 清除未使用到的依赖

    go mod tidy       

    原创文章,作者:VHMOK,如若转载,请注明出处:https://www.beidanyezhu.com/a/30024.html

    (0)
    VHMOK的头像VHMOK
    上一篇 2025-02-16 18:24:04
    下一篇 2025-02-16

    相关推荐

    分享本页
    返回顶部