module name and module import path in Go

· 353 words · 1 minute read

1. golang管理依赖 🔗

  Modules are how Go manages dependencies.

1.1 module and packages 🔗

  A module is a collection of packages.

1.2 workspace 🔗

 A workspace is a collection of modules.
 A workspace can be declared in a go.work file.
 go env GOWORK, go work init, go work use

2. module name 🔗

 go.mod文件2个作用。

  • 在第一行 module xxx/yyy/ccc 定义了module的module_import_path。 go mod init xxx/yyy/ccc
  • 声明本module需要的依赖项目。
The module path becomes the import prefix 
for all packages the module contains.

3. module import path 🔗

  • Importing packages from your module
    • import path = module_import_path + folder_name
    • import后面的最后一个元素是路径的目录名称,并非包名。
      • 一般情况下是最后一级的目录名称和包名一样。
      • 不一样时import path, 但代码中使用包名。
  • Importing packages from remote modules
    • 下载到$GOPATH/pkg/mod
    • 记录到go.mod, go.sum

4. code organization 🔗

  an go source code file 只可能包含<package> + <import> + <var> + <const> + <type> + <Func>

Functions, types, variables, and constants defined in one source file 
are visible to all other source files within the same package.

5. reference 🔗

[1] go.mod file reference -> https://go.dev/doc/modules/gomod-ref

[2] using Go Modules -> https://go.dev/blog/using-go-modules

[3] go code organization -> https://go.dev/doc/code

[4] Go Modules Reference -> https://go.dev/ref/mod