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

About Golang-import import package syntax

藏色散人
Release: 2021-05-07 09:10:45
forward
1952 people have browsed it

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")
Copy after login
Then in The code can be called in the following ways:

fmt.Println("hello world")
Copy after login

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"
Copy after login
//2.绝对路径//加载GOPATH/src/shorturl/model模块//简单理解就是:项目名/包名import   "shorturl/model"
Copy after login

Special import syntax for package

Point operation

import (
    . "fmt")
Copy after login

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")
Copy after login

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")
Copy after login

_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!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template