This is a creation in
Article, where the information may have evolved or changed.
4th: Basic structure and basic data types
The go source file ends with a. Go, the file name cannot contain spaces and other special characters, but can be underlined
Code is case-sensitive and there's nothing to say, note the identifiers
Valid identifiers must begin with a character (which can use any UTF-8 encoded character or _), followed by 0 or more characters or Unicode digits
For example: x56,group1,_x23,i,өԑ12
A few examples of not working
The following are invalid identifiers:
1AB (starting with a number), Case (keyword for Go language), a+b (operator is not allowed)
Go 25 keywords or reserved words
| Break |
Default |
Func |
Interface |
Select |
| Case |
Defer |
Go |
Map |
struct |
| Chan |
Else |
Goto |
Package |
Switch |
| Const |
Fallthrough |
If |
Range |
Type |
| Continue |
For |
Import |
Return |
Var |
Go also has 36 predefined identifiers that represent some of the basic types and functions
| Append |
bool |
Byte |
Cap |
Close |
Complex |
Complex64 |
complex128 |
UInt16 |
| Copy |
False |
Float32 |
Float64 |
Imag |
Int |
int8 |
Int16 |
UInt32 |
| Int32 |
Int64 |
Iota |
Len |
Make |
New |
Nil |
Panic |
UInt64 |
| Print |
println |
Real |
Recover |
String |
True |
UInt |
Uint8 |
UIntPtr |
Concept, import and visibility of packages
Each go file belongs to and belongs to only one package, and the first line of non-comment in the source file specifies the package name, and several go files can be in a package, because the package name and file name are not the same.
Main package Comparison Special packages main represents a program that can be executed independently, each Go application contains a package named main
Go compilation process is very special
The package model in Go uses an explicit dependency mechanism for fast compilation purposes, and the compiler .o extracts the information of the transitive dependency type from the object file with the suffix name (required and only this file is needed).
If A.go dependent B.go , but B.go dependent on C.go :
- Compile
C.go , and B.go then yes A.go .
- In order to compile
A.go , the compiler reads B.o instead C.o .
This mechanism can significantly increase compilation speed when compiling large projects.
You can import a package by using the Import keyword in the following ways
Import "FMT"
Import "OS"
Import "FMT"; Import "OS"
Import (
"FMT"
"OS"
)
Import ("FMT", "OS")
Note that if the package name does not start with. or/, go will re-locate the global file, such as "FMT", "aaa/bbb" if you start with./, go will look in the relative directory if only/start will be found in the absolute path
The visibility rules for go are fairly simple, if you start with a capital letter (function, variable, and so on), the other package is visible, and lowercase is the equivalent of the package's private bar
You can also alias when importing a package Oh, for example: Import FM "FMT" can be used to use FM as FMT.
The variable that is famous after import is the global variable of the package
Here again to explain the function of Go, type and comment, also very simple feel there is no good to remember, because the use of more, it seems to be taken for granted
However, there are a few points to note:
1.go does not allow implicit type conversions,
The execution (program start) sequence of the 2.Go program is as follows:
- Import all other packages that are referenced by the main package sequentially, and then perform the following processes in each package:
- If the package imports another package, it executes recursively from the first step, but each package is imported only once.
- Constants and variables are then initialized in the reverse order in each package, and the function is called if it contains an init function.
- After doing this, main executes the same procedure, and finally calls the main function to start executing the program.