golang introduction and create a package

. 1  
2  / *
 3      single packet:
 . 4          Improt "directory path to the package"
 . 5      a plurality of packets:
 . 6          Improt ( "package directory path", "package directory path")
 . 7          Improt (
 . 8              "package directory path"
 9              " directory path to the package "
 10          )
 11      package names referenced custom:
 12 is          Import (
 13 is              " directory path to the package "
 14              " Crypto / RAND "
 15              custom name" path "// package directory name is replaced mrand avoid conflicts
 16              R & lt "Crypto / RAND"
 . 17          )
 18 is      introduced only packet without using the package:
19         Import (
 20 is              _ "path / to / Package"
 21 is          )
 22 is      initialized inlet: init ()
 23 is          each source can use an init () function.
24-          the init () function (before the function executes main ()) is called automatically before the program execution.
25          calls for the main reference sequence () of the package, depth-first order to the initialization.
26          during operation, is introduced into the final package will be initialized first and calls init () function: main → A → B → C , then the packet init () function call order: C.init → B.init → A → main .init
 27          plurality of the same package init () function call to the unpredictable order.
28          the init () function can not be other function calls
 29  
30          For example, in order to improve the efficiency of the math library trigonometric calculations can, when the program starts
 31          will be completed ahead of schedule index value of trigonometric tables in external program memory look-up table way to quickly obtain the value of trigonometric functions.
32         But calls the initialization function of the index table of trigonometric not want to use an external trigonometric by each developer calls,
 33          if there is a mechanism to tell the trigonometric functions program package in the bag when to start trigonometric functions, then we can solve the initialization the problem
 34 is  * / 
35  
36  Package info_packge
 37 [
. 1  / * 
2  golang incorporated custom package rules:
 . 3      1: Set $ GOPATH environment variable
 . 4      2: Custom package inside the API functions provided externally, to be the first letter capitalized
 . 5      3: compile and install the package:
 . 6          .go package file must be stored in a separate folder (e.g., test)
 . 7          then go build using the test folder and go install command: at $ GOPATH added after generating pkg folder
 . 8          is generated test.a file folder ( Therefore, the folder of the parent package and the package name is preferably the name of the same)
 . 9      4: package:
 10              generates the real name and the name of the package bag may be inconsistent
 . 11              Project / the src / Test / my.go
 12 is               main.go
 13 is              PKG / XXXXXX / test.a
 14  
15  * / 
16 Package info_packge

 

 

Guess you like

Origin www.cnblogs.com/weihexinCode/p/12317052.html