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 .