This is a created article in which the information may have evolved or changed.
Recently in the study of the Golang of the creation of custom packages, generated some experience, so this article as a record.
We add the custom package to our project's main function by using the import statement, and the parameters after the import statement should be the name of the folder , and the package name of the custom packages or the file names in the custom package. For example, the file structure directory is as follows:
Source code for Main.go:
package mainimport "fmt"import "foo"func main() {bar.Abc()fmt.Print("This is main\n")}
Source code for Foo/test.go:
package barimport "fmt"func Abc() {fmt.Print("This is test print\n")}
The above code is successfully compiled. We can summarize the following points:
(1) The import statement uses the name of the folder
In the example above, the import parameter corresponds to the folder foo
(2) The name of the folder and the package name are not necessarily the same
In the example above, the folder is Foo, and the package name is bar.
(3) How to invoke custom package usage package名称.函数名
For example, the bar used above. ABC ().
(4) There is no relationship between the call and the file name of the custom package
For example, the above Test.go file, if changed to Test_abc.go, the program will compile properly. The compilation system automatically finds all files under the Foo folder, finds the package bar in it, and then selects the ABC function.