Import in Go lang

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

Import

We are writing Go code is often used to Import This command is used to import package files, which we often see in the following ways:

Import
"FMT"
)

Then we can call the code in the following way

Fmt. Println ("Hello World")

above this FMT is a Go the standard library of language is actually going to GOROOT environment variable Specifies the directory to load the module down,


of course Go of the Import It also supports the following two ways to load the modules you write:

1. relative Path

import "./model"// the current file in the same directory Model directory, but this is not a recommended way to Import

2. Absolute Path

import "Shorturl/model"// Loading Gopath/src/shorturl/model Module

The above shows some Import commonly used in several ways, but there are some special Import , let a lot of novice very confusing, below we come one by one explain what is the matter

1. Point Operation

We sometimes see the following ways to import packages

Import
. "FMT"
)

this point operation means that the package is imported after you when calling the function of this package, you can omit the prefix of the package name , which is what you called earlier FMT. Println ("HelloWorld") can be omitted as written Println ("HelloWorld")

2. alias Operation

Alias operation as the name implies, we can name the package another one that we remember easily.

Import
F "FMT"
)

alias operation the prefix becomes our prefix when calling the package function, i.e. f.println ("HelloWorld")

3 . _ operation

This operation is often confusing for many people an operator, see the following Import

Import (
"Database/sql"
_ "Github.com/ziutek/mymysql/godrv"
)

_ The operation is actually to introduce the package, instead of directly using the function inside the package, instead of invoking the package inside the Init function .


Related Article

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.