golang package notes

Package is used to manage and maintain the organizational structure of golang projects, an effective means to classify and isolate variables, define data structures, methods, and functions

Features are as follows

        1. Similar to Class in java (of course some people will say that struct is like, but it depends on your perspective), my understanding is that variables, methods, and functions are all separated by packages, such as variables with the same name, Methods etc. can be defined and effectively isolated

        2. Packages are managed in units of directories, and multiple go files in a directory must belong to the same package

        3. Different directories or subdirectories can theoretically define packages with the same name (such as package apkg), but it is absolutely not recommended in actual use, which will cause the following problems

                a. Doing this in the first place makes package management look confusing, poorly read, and difficult to maintain

                b.apkg can be defined in different directories, but when the same package is referenced, it will compile and report an error

                c. On the premise of the situation described in b, what should I do if I have to quote in the same package? I only need to give an alias when citing the package. From this, if the package that cites other different projects has the same name, it can also do the same

                        Syntax: import aaa "xxprj/xx directory/abc"

        4. When the package is referenced, you can give an alias, for example

                Define package: package abc

                There are two ways to cite:

                                import "xxprj/xx directory/abc"

                                import aaa "xxprj/xx directory/abc"                                

Guess you like

Origin blog.csdn.net/m0_37298500/article/details/125942247