1. Golang built-in types and functions

1.1. Built-in types 1.1.1. Value types: bool int(32 or 64), int8, int16, int32, int64 uint(32 or 64), uint8(byte), uint16, uint32, uint64 float32, Float64 String Complex64, complex128 array – array of fixed length 1.1.2. Reference types :(pointer types) slice — sequence array (most commonly used) map — map chan — pipeline 1.2. Built-in functions The Go language has several built-in functions that can be used without import operations. They can sometimes operate on different types, such as Len, Cap, and Append, or must be used for system-level operations, such as Panic. Therefore, they require direct compiler support.

Append -- used to append elements to arrays, slice, return modified arrays, slice close -- used mainly to disable channel delete -- remove value panic corresponding to keys from map -- stop regular goroutine Recover -- allows the program to define goroutine's panic action real -- returns the real part of complex (complex, real imag: Imag -- returns the imaginary part of complex make -- allocates memory, returns Type itself (applies only to slice, map, channel) new -- allocates memory, mainly allocates value types such as int and struct. Return a pointer to Type cap -- capacity returns the maximum capacity of a Type (for slices and maps only) copy -- used to copy and join slices, returns the number of copies len -- to find the length, For example, string, array, slice, map, channel, return length print, println -- the underlying printing function. FMT package is recommended in deployment environmentCopy the code

Error type error interface {// As long as the error () function is implemented, the return value of String is implemented err interface

        Error()    String

}
Copy the code
  1. Init function and main function

The init function is used to initialize packages in go. This function is an important feature of go.

It has the following characteristics:

1 The init function is used to initialize the package before the program is executed. 2 Each package can have multiple init functions. 3 Each source file of a package can also have multiple init functions. 4 The execution sequence of multiple init functions in a package is not clearly defined in the GO language 6 The init function cannot be called by other functions. Instead, it is automatically called before the main function is executedCopy the code

The Go program’s default entry function (main function) : the body of the func main() function is wrapped in {} parentheses.

Func main(){// Function bodyCopy the code

1.3. Similarities and differences between init and main: The two functions cannot be defined with any parameters or return values, and Go is called automatically. Differences: Init can be applied to any package, and multiple can be defined repeatedly. The main function can only be used in the main package, and only one can be defined. The order in which the two functions are executed:

Init () calls to the same Go file are ordered from top to bottom.

For different files in the same package, the init() function of each file is called in the order of “from smallest to largest” according to the filename string.

For packages that do not depend on each other, init() in the main package is called in the order of “import first then call”. If the package has dependencies, init() in the earliest dependent package is called first, and main function is called last.

If you use println() or print() in your init function, you’ll notice that they don’t execute in the order you expect them to. These two functions are officially recommended for use only in test environments, not in formal environments.