4 ways to import the package in Golang

Source: Internet
Author: User
  • Generic Import method
    import (   "fmt")
    Then we can call the corresponding function or variable through the package name.
    fmt.Println("Hello World!")
  • Click Import
    Click Import is to add a "." in front of the import package, this import method, when using the corresponding package function or variable, you can omit the package name
    import (   . "fmt" )
    Then we can call the corresponding package function without the package name prefix
    Println("Hello World!")
  • Alias Import
    Sometimes the name of the package may be long or not easy to remember, then you can give the package an individual name (such as a short ah or easy to remember AH)
    import (  f "fmt")
    You can then use the package as follows
    f.Println("Hello World!")
  • Underline import
    When we import a package, all of its init () functions are executed, but sometimes we don't really need to use these packages, just want the init () function inside it to be executed, this time, you can use the underscore import
    import (   "database/sql"    _ "github.com/go-sql-driver/mysql")

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.