The syntax and some special usage of golang import package


Import syntax of package

When you write go code, you often use the Import command to import packages. Refer to the following:


Then it can be called in the following ways in the code:

fmt.Println ("I love developeppaer")

FMT is the standard library of go. It actually loads the module from goroot. Of course, the import of go also supports the following two ways to load modules written by oneself:

Relative path

Import "." / model "// the model directory of the same directory as the current file, but it is not recommended to import in this way

Absolute path

Import "shorturl / model" // load gopath / SRC / shorturl / model module

Special usage of package import

The above shows some common ways to import, but there are some special import, which makes it difficult for many novices to understand. Here are three ways to use import packages.

Point operation

Sometimes you can see the following ways to import packages:

  . "fmt" 

The point operation means that after the package is imported, when you call the function of the package, you can omit the prefix package name, which is what you called earlier:

fmt.Println ("I love developeppaer")

It can be omitted as follows:

Println ("I love developeppaer")

Alias operation

As the name suggests, the alias operation can name the package another name that is easy to remember:

  f "fmt" 

When the alias operation calls the package function, the prefix becomes the renamed prefix

f. Println ("I love developeppaer")

Underline operation

This operation is often a confusing operator for many people. Please see the import below

import ( 
  _ “” 

Glide line The operation is just to introduce the package. When init () is imported, it will not be executed by all package functions. In this case, you can use the “_” The operation refers to the package. That is, use the “_” The operation reference package cannot call the exported function in the package through the package name, but simply to call its init() function.

For more information on the golang import import package syntax and some special uses, see the related links below

Recommended Today

Git usage specification

​ 1. Basic operation of GIT -Git init if a project needs to be managed using git, it needs to be initialized -Git status: check the status of the current code (red: in the Development Zone, green: in the temporary storage zone, nothing to commit: there is no change in the Development Zone) -Git checkout […]