laitimes

Xiaoxin shared: The introduction of the GO language pack

Xiaoxin shared: The introduction of the GO language pack

Share the fun, spread the joy,

Gain knowledge and leave a good future.

Dear You,

This is LearingYard Academy!

Today, Xiaobian brings you the introduction of GO language packs

Welcome to visit!

Share the fun, spread the joy,

Gain knowledge and leave a good future.

Dear You,

This is LearingYard!

Today, the editor brings you Introduction of Go language pack

Welcome to visit!

Mind mapping

Mind mapping

Xiaoxin shared: The introduction of the GO language pack

1. How to introduce the package

1.Introduction method of package

In Go, the ingestion of packages is achieved through the import keyword. You can use the import statement to import an external package into the current program. For example:

In Go language, introducing packages is achieved through the import keyword. You can use the import statement to import external packages into the current program. For example:

goimport "fmt"
           

This statement imports the FMT package from the standard library into the current program, so that the program can use the functions and methods defined in the FMT package.

This statement imports the fmt package from the standard library into the current program, allowing the program to use the functions and methods defined in the fmt package.

2. The path and alias of the package

2.Path and alias of the package

The package path in Go refers to the path of the package in the file system, usually associated with the package name. IF THE PACKAGE YOU WANT TO IMPORT IS NOT IN THE STANDARD LIBRARY OR IS LOCATED OUTSIDE THE PATH SPECIFIED BY GOPATH OR GOMODULE, YOU NEED TO USE THE FULL PACKAGE PATH. For example:

The package path in Go language refers to the path of the package in the file system, usually associated with the package name. If the package to be introduced is not in the standard library or outside the path specified by GOPATH or GOMODULE, the complete package path needs to be used. For example:

goimport "github.com/gin-gonic/gin"
           

Sometimes you can use aliases to simplify ingestion, especially if the imported package names are too long or conflicting:

Sometimes aliases can be used to simplify the introduction, especially when the package name introduced is too long or conflicting:

goimport gin "github.com/gin-gonic/gin"
           

This makes it possible to use gin in place of the full package name github.com/gin-gonic/gin in your code.

This way, you can use 'gin' instead of the full package name 'GitHub/gin gonic/gin' in the code.

3. Initialization of the package

3.Initialization of the package

When a package is introduced, its global variables and init functions are automatically executed. These global variables and init functions are executed in order from top to bottom, and are initialized only once per package. This mechanism ensures that package initialization is controllable and the sequence is predictable. For example:

When a package is introduced, its global variables and init function will be automatically executed. The execution order of these global variables and the init function is from top to bottom, and each package will only be initialized once. This mechanism ensures that the initialization of packets is controllable and the order is predictable. For example:

gopackage main

import (
    "fmt"
    "github.com/example/externalpackage"
)

var globalVar = "Initialized"

func init() {
    fmt.Println("Initialization of main package")
}

func main() {
    fmt.Println("Main function")
    fmt.Println(globalVar)
    externalpackage.ExampleFunc()
}
           

In the example above, after the main package is introduced, globalVar will be initialized as "Initialized", and the init function will be executed and an initialization message will be output.

In the above example, after the main package is introduced, GlobalVar will be initialized to "Initialize", and the init function will be executed and output an initialization message.

That's all for today's sharing.

If you have a unique idea for today's article,

Welcome to leave us a message,

Let's meet tomorrow,

Have a great day!

That's all for today's sharing.

If you have a unique idea for today's article,

Welcome to leave us a message,

Let's meet tomorrow,

Have a great day!

This article was written by LearingYard, if there is any infringement, please contact us.

Some of the reference content comes from Baidu

Translation source: Google Translate

Editing, typesetting|Xiaoxin

Audit|S70