go mod init

Enable dependency tracking

When your code imports packages contained in other modules, you manage those dependencies through your code's own module.

That module is defined by a go.mod file that tracks the modules that provide those packages. That go.mod file stays with your code, including in your source code repository.

go mod init命令用于在您的 Go 项目中初始化一个新模块。go.mod它在运行命令的目录中创建一个新文件。

pathgo mod init path模块路径。模块路径是模块内所有包共享的导入路径前缀。它通常是托管模块代码的存储库位置。

例如,如果您正在开发一个计划托管在 的包github.com/yourusername/yourproject,您将运行go mod init github.com/yourusername/yourproject.

如果您不打算发布模块,通常使用未绑定到特定主机的模块路径。这可以像项目名称本身一样简单。例如,go mod init yourproject

此命令创建的文件go.mod用于管理和跟踪 Go 项目的依赖项。