Home>Article>Backend Development> What is the difference between "." and "_" when importing packages in go language?
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.
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:
After importing usage points:
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!