The package is an integral part of the go language, defined in the first line of each go source code, defined by the package name, which is the name used when calling the package.
Package Concept Summary:
- Each Go file belongs to and belongs to only one package. A package can consist of many
.go source files as extensions, so the file name and package name may be different, in order to standardize, function similar or belong to the same nature of the source code to the same package name.
- Each application must have a package name of main, which is the entry for the program, and the execution will look for code with the package name main.
- Packages can be called to each other, primarily to reduce code repeatability
- Once the package is introduced, it needs to be used, unless it is introduced, ignoring it (with the exception of the preceding _ means ignoring it, just initializing it), if the package is introduced and is not used in the code, the runtime will error
| Second, the introduction of the package |
1. How the package is introduced
Method One:
" FMT " "os"
Method Two:
" FMT " " OS "
Method Three (recommended!) )
Import ( "fmt" "os")
2. Introducing Additional Packages
In the go language, the path to the package is introduced according to the GOPATH/SRC environment variable as a relative path, if gopath exist more than one, then go will find the path, until found, if GOPATH/SRC not found the compilation error.
Example:
My Gopath is: C:\Users\17647\Desktop\go_work\src
First build the package file 1:c:\users\17647\desktop\go_work\src\day02\eg1\pkg.go (the path to GOPATH/SRC is Day02\eg1\pkg.go)
The contents are as follows:
Package Add " FMT " intint) { varint= a + b FMT. Println ("res", c)}
The following is introduced in main:
Package Mainimport ( "fmt" "day02/eg1 " / */) func main () { Add. Sum (2,3/**/ FMT. Println ("Hello, world! " )}
Icon:
1. Declaration of a single variable
In the go language, all variables must first be declared in use, and the following are declared variables and assignment methods:
- Assign values First: var < variable name > < variable type > Assignment: Variable assignment format: < variable name > = < expression >
- Simultaneous assignment of declarations: var < variable name > < variable type > = < expression > (variable name: = Expression)
Example:
Package Mainimport"FMT"Func Main () {varAstring /*declares a variable with a variable named A and a string of type*/a="WD" /*assigning values to a variable*/ varAgeint= A /*declaring and assigning a value variable*/FMT. Println (a,age)}2. Multiple variable declarations
- First declaration after assignment: var < variable 1>,< variable 2>,< variable 3> < variable type > assignment: < variable 1>,< variable 2> = value 1, value 2
- Declaration assignment at the same time: var < variable 1>,< variable 2> = value 1, value 2 can be abbreviated as: < variable 1>,< variable 2> = value 1, value 2
- Multiple variables can also be declared with VAR ()
Example:
Package Mainimport"FMT"Func Main () {varA, bintA, b=1,2 varC,d =3,"WD" /*Type automatic Inference*/e, F:=4,"Hello" /*Shorthand*/FMT. Println (a,b,c,d,e,f)}
Using the Var () declaration
" FMT " func main () { var ( int // default = 0 string // The default value is an empty string ("") 3 ) FMT. Println (a,b,c)}
3. Variable name
Naming principles:
- The first character can be any Unicode character or underline
- The remaining characters can be Unicode characters, underscores, numbers
- cannot be used as a variable name with the following keyword
Break default Func Interface Selectcase defer go map struct Chan Else Goto Package Switchconst fallthrough if range Type continuefor import return var
4. Visibility
Visibility is visible to the outside of the package, and it is allowed to be visible (accessible) when other packages invoke the variables of the current package.
- Variable starts with uppercase characters, indicating visible
- Variable starts with a non-uppercase letter, it is private, not visible
Go language Trip-Package and variable