Home >Backend Development >Golang >About Golang-import import package syntax

About Golang-import import package syntax

藏色散人
藏色散人forward
2021-05-07 09:10:451950browse

The following tutorial column will introduce you to the Golang-import import package syntax. I hope it will be helpful to friends who need it!

About Golang-import import package syntax                                                                                                                

The import command is often used in Go code to import package files. The reference is as follows:
import (
    "fmt")
Then in The code can be called in the following ways:

fmt.Println("hello world")

The fmt above is the standard library of the Go language. It actually goes to GOROOT to load the module. Of course, Go's import also supports the following two methods to load the modules written by yourself. Module:

//1.相对路径//当前文件同一目录的model目录,但是不建议这种方式importimport   "./model"
//2.绝对路径//加载GOPATH/src/shorturl/model模块//简单理解就是:项目名/包名import   "shorturl/model"

Special import syntax for package

Point operation

import (
    . "fmt")

Point operation The meaning is that after this package is imported, when you call the function of this package, you can omit the prefix of the package name, that is, fmt.Println("hello world") can be omitted and written as Println("hello world").

Alias ​​operation

import (
    f "fmt")

Alias ​​operation is to name the package another name that is easy to remember. When calling the package function, the prefix becomes Without the rename prefix, that is, fmt.Println("hello world") can be omitted and written as f.Println("hello world").

_Operation

import (
    "database/sql"
    _ "github.com/ziutek/mymysql/godrv")

_OperationActually just 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

_operation

to reference the package. Even if you use _operation to reference a package, you cannot call the exported function in the package through the package name, but just to simply call its init function ().

The above is the detailed content of About Golang-import import package syntax. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete