This is a creation in Article, where the information may have evolved or changed.
Recently learning go programming, this article simply describes how to use packages (package management) in Go programming.
Like most other languages, go also has packages and defines a package using the Packages keyword. First, introduce how to introduce the package into the program, and introduce the package in the following ways:
1. The simplest way to introduce a package is to introduce the package directly, for example:
Import "FMT"
Import "OS"
2. The package can also be introduced in the following way, and written in parentheses:
InPort (
"FMT"
"OS"
)
In this way, you can introduce a system package or a third-party package, and the following highlights how to introduce custom packages and functions:
In general, we put the main program under the Mian folder of SRC (the main program contains the main function, and the package name of the main program is written as the bundle main), and the other modules are placed in the appropriate folder, for example
The main function in the Main.go file, the main function name can also be other, but must contain the main function. In go programming, how do you introduce your own modules, such as how to invoke Add.go, Subtract.go, or multiply.go files in Main.go.
Add.go and Subtract.go are under the Cal folder, so the packages for both programs are named CAL (Package cal), Multiply.go under the Multi folder, so the package name of the program is multi (packages multi). If the Mian function is to invoke a function in Add.go or SUBTRACT.GO, the package "Cal" (import "cal") must be introduced. To invoke a function in Multiply.go, it is necessary to introduce the package "multi", if we write the import "multi" directly in the program, the compiler will prompt us can not find the "multi". Since our "Multi" package is under the package "Cal", we are going to write the package name in full "Cal/multi", and we can invoke the functions in each file.
If the first letter of the function name is capitalized in go, it means that the function is public and can be called by other programs, and if the first letter is lowercase, the function is private, so we can only invoke public functions in Add.go, Subtract.go, or Multiply.go. Specific calls such as:
Add.go
Subtract.go
Multiply.go
Last Note: The file name can be inconsistent with the package name, but the package name used in the files must be the same as the package name.