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

nuxt.js Imitating wechat app communication chat | Vue + nuxt chat | imitating wechat interface

Project overview be based on vue.js + nuxt.js +Chat room imitating wechat app interface developed by vuex + webpack + node + vant and other technologiesNuxtchatroom project。 It realizes the functions of card type drag and slide, message / expression sending, picture / video preview, red packet / circle of friends and so on. Technology […]