Go 项目写 demo

对一个本地的 Go 库写 demo, 应该如何配置:

  1. 新建一个目录,包含包文件夹

  2. 在与包文件夹 example 同目录下建立 main.go, 然后在其中导入模块名 import ("example")

  3. 在当前目录下,

    go mod init module_name 初始化 go.mod 添加下面语句到 go.mod,从而导入本地的包 example, replace 最后为相对路径.

    1
    2
    require example v0.0.0
    replace example v0.0.0 => ./example

  4. 执行 go mod tidy 删除不需要的依赖包、下载新的依赖包、更新 go.sum

  5. 运行 main.go

参考

  1. missing go.sum entry for module providing package - 升级打怪 - 博客园

  2. build 项目时出现报错--本地开发的包导入: missing go.sum entry for module providing package_奔跑的 Camille 的博客-CSDN 博客_c 语言 build 时出错