Home  >  Article  >  Backend Development  >  What is the difference between "." and "_" when importing packages in go language?

What is the difference between "." and "_" when importing packages in go language?

青灯夜游
青灯夜游Original
2020-12-08 11:47:274114browse

Difference: The "_" operation actually only introduces the package; using the "_" operation to reference the package cannot call the exported function in the package through the package name, but is just to simply call its init() function . The meaning of the "." operation is that after this package is imported, when you call the function of this package, you can omit the prefixed package name.

What is the difference between

Demo environment for this article: Windows 7 system, Go1.11.2 version, Dell G3 computer.

Related recommendations: "golang tutorial"

When importing packages in golang, we sometimes encounter the need to add an underscore or Click, I never understood what it meant, and then I searched:

The package is preceded by an underscore_: The _ operation is actually just to introduce the package. When a package is imported, all its init() functions will be executed, but sometimes you don't really need to use these packages, you just want its init() function to be executed. At this time, you can use the _ operation to reference the package. Even if you use the _ operation to reference a package, you cannot call the exported functions in the package through the package name, but just to simply call its init() function.

There is a dot before the package:

import(.“fmt”)

The meaning of this dot operation is that after the package is imported, when you call the function of this package, you can omit the prefixed package Name, that is, the fmt.Println("hello world") you called earlier can be omitted and written as Println("hello world")

Before the import point of use:

What is the difference between

What is the difference between

After importing usage points:

What is the difference between

What is the difference between

For more programming related knowledge, please visit : Programming Video! !

The above is the detailed content of What is the difference between "." and "_" when importing packages in go language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn