Getting to Know Go Language 20-Packages and Engineering [Using go mod to manage projects, package import rules, init call chain, visibility]


Packages and Engineering

Manage projects with go mod

  Initialize the project:

go mod init $module_name

$module_name and directory name can be different. The above command will generate a go.mod file, the contents of which are as follows:

module go-course

go 1.17

require (
    github.com/ethereum/go-ethereum v1.10.8
    github.com/gin-gonic/gin v1.7.4
)

  Go looks for dependent packages from the current project, GOROOT, and GOPATH in turn.

  1. Find the go.mod file step by step from the directory where the current go file is located (assuming go.mod is located in the directory mode_path), which defines module_name, then the path to import the package is "module_name/path of the package relative to mode_path".
  2. The packages provided by the go standard library are under GOROOT/src.
  3. Third-party dependent packages are under GOPATH/pkg/mod.

  Starting from go1.7, go get is only responsible for downloading the third-party dependency package and adding it to the go.mod file, and go install is responsible for installing the binary file.

  • go get github.com/mailru/easyjson will generate the github.com/mailru/easyjson directory under the GOPATH/pkg/mod directory.
  • go install github.com/mailru/easyjson/easyjson will generate easyjson binary executable file under GOPATH/bin.

  go mod tidy scans all code in the current project to add undocumented dependencies to the go.mod file or remove unused dependencies from the go.mod file.

Package Import Rules

package declaration

  • The first line of the go file declares package xxx.
  • Comments about the package can be written above the package declaration, and package comments can also be written specifically in doc.go.
  • The package name can be different from the directory name.
  • In the same directory, the package names of all go files must be consistent.

package reference

  • You can directly use variables, functions, and structures in other go files in the same directory.
  • For cross-directory use, you need to add the package name before the variable, and import the directory where the package is located.
imoprt "go-course/package"      //go-course是model名,package是目录名
mypackage.Add()     //mypackage是包名,它对应的目录是package
  • In the import block, you can refer to the parent directory, or you can refer to the subdirectory.
  • Reference relationships cannot form a cycle.
  • An alias can be given to the package in front of the import directory.
imoprt asd "go-course/package"
asd.Add()

init call chain

  The main function is the only entry point of the go program, so there can only be one main function. The main function must be located in the main package. The init() function is executed before the main function is executed. In a directory, or even a go file, init() can be defined repeatedly. When importing other packages, the init() function in the corresponding package will also be called before the main() function.

insert image description here

import _ "net/http/pprof"

  Add a _ in front of the directory, but the code does not explicitly use the functions or variables in this package. In fact, it wants to execute the init() function in this package.

visibility

  • Functions, variables, and structures named starting with a lowercase letter can only be accessed within this package.
  • Functions, variables, and structures named starting with a capital letter can also be accessed in other packages.
  • If the structure name starts with an uppercase letter, and its member variables and member methods start with a lowercase letter, such members can only be accessed within this package.

  The package named internal in Go can be accessed by the level directory and the upper-level directory, and the upper-level directory cannot be accessed. As shown in the figure below, the c directory (the upper level directory of internal) and its descendants can be imported arbitrarily, but the a directory and b directory cannot import internal and all its subordinate directories.

insert image description here

insert image description here

Guess you like

Origin blog.csdn.net/m0_52896752/article/details/130461073