Golang Import Underline effect

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Import is the role of importing other package, but today in the view of the Beego framework to see the import underline, do not know its meaning, so Baidu and solution.

import underscores (e.g. import _ Hello/imp): When importing a package, All init () function will be executed, however, there are times when we don't need to import the entire package, just want it to execute the init () function. You can use import to refer to the package at this time. Even if you refer to the package with the import _ package path only to invoke the Init () function, you cannot call other functions in the package by the package name.

Example
Directory structure:
Gopath
--bin
--pkg
--src
Main.go
--hello
--imp
Init.go

Main.go

1Package Main23Import _"Hello/imp"45Func Main () {6//Imp. Print () Compile error, said: Undefined:imp7}

Output: Imp-init () come here. 1Package Imp23Import"FMT"45func init () {6Fmt. Println ("Imp-init () come here.")7 }89func Print () {TenFmt. Println ("hello!") One}

As for the import point operation and alias operation is quite simple and understanding, so no longer demonstrated.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.