当前位置:网站首页>Use of go package
Use of go package
2022-07-02 03:53:00 【UPythonFish】
List of articles
go Use of the bag
brief introduction
In Engineering Go Language development projects ,Go The source reuse of language is based on the package (package) Based on . package
There are many Go Source code collection , It's an advanced code reuse scheme ,Go Language provides us with many built-in packages , Such as fmt
、os
、io
etc. .
Definition package
Packages make it easy for us to organize code , Write the same type code under a package , Easy to manage . A package can be simply understood as a storage .go File folder , All under this folder go file , You need to add the following code in the first line of the code , Declare that the file belongs to that package .
package Package name
matters needing attention :
- There can only be one package under a folder , Files of the same package cannot be in multiple folders .
- The package name can not be the same as the folder name , Package name cannot contain
-
Symbol . - Package name is main The package of is the entry package of the application , Compile without main Package source code will not get executable .
visibility
If you want to refer to an identifier in another package in one package ( Such as variable 、 Constant 、 type 、 Functions, etc ) when , The identifier must be visible (public). stay Go In a language, you only need to capitalize the initial of an identifier to make it visible to the outside world .
package test
import "fmt"
var a = 100 // Initial lowercase , The external package is not visible , Can only be used in a package
const Mode = 200 // title case , Can be used in other packages
func main() {
}
func Test(){
fmt.Println(" Can be used in external packages ")
}
func test() {
var Age int = 18
fmt.Println(Age) // Function local variable , The external package is not visible , Can only be used in the current function class
fmt.Println(" Can only be used in a package ")
}
Field names in structs and method names in interfaces are capitalized , External packages can access these fields and methods . for example :
type Student struct {
Name string // Methods that can be accessed outside the package
class string // Only fields accessed within the package
}
type Payer interface {
init() // Only methods accessed within the package
Pay() // Methods that can be accessed outside the package
}
Package import
Reference the contents of other packages in the code , Need to use import The package used for keyword import . The specific syntax is as follows :
import " The path of the package "
import "day02/package1" // Single line import
import ( // Multiline import
"day02/test"
"fmt"
)
matters needing attention :
- import The import statement is usually placed below the package declaration statement at the beginning of the file .
- The imported package name needs to be wrapped in double quotes .
- Recommended go model Management package ,go path It's from
$GOPATH/src/
And then we start to calculate , Use/
Path separation . - Go It is forbidden to import the package circularly in the language .
- The package name and the folder where the package is located can be different , But it is recommended not to modify , use The package name is just the file name
- Use the identifier in the import package , Should take Package name . identifier Way to use
// Create a folder for test,test Create s2 file
// package test // The default package name is the same as the folder name test
package t1
const Mode = 200 // title case , Can be used in other packages
// stay main The following is used in the package
package main
import (
"day02/test" // The path of the package
"fmt"
)
func main() {
fmt.Println(t1.Mode) // Package name . identifier
}
main Functions and main package
All executable Go The program must contain a main function . This function is the entry to the program .main Functions should be placed in main In bag .
In a project , Only one is allowed main function , There is only one entry , Usually run a single go file , File required (file) Form operation .
package main
func main() {
}
init function
stay Go When a language program executes, the import package statement will automatically trigger the inside of the package
init()
Function call . It should be noted that :init()
The function has no arguments and no return value .init()
Function is automatically called to execute when the program is running , You can't call it in your code .
func init(){ // Sense and python in __init__ Almost the same effect
}
init Functions can be used to perform initialization tasks , It can also be used to verify the correctness of the program before starting execution .
The initialization sequence of packages is as follows :
- First initialize the package level (Package Level) The variable of
- Next call init function . Bags can have multiple init function ( In one file or distributed in multiple files ), They are called in the order that the compiler parses them .
If one package imports another , The imported package will be initialized first .
Although a package may be imported multiple times , But it will only be initialized once .
Go
The compiler then builds a tree of package references , Then determine the compilation order according to the reference order , Compile the code of these packages in turn .At run time , The last imported package will be initialized and called first
init()
function , As shown in the figure below :
go path turn go moduls
go stay 1.11 Support later go moduls Pattern , It is also the recommended package management mode
If you use go path Model for development , All project codes must be placed in go path The next path src Under the document , Otherwise, we can't find , Including built-in package , The third package , Therefore, it is extremely inconvenient .
however , Inevitably, some old projects used go path Management package , therefore go path turn go moduls It is necessary to .
Use go moduls, The code does not need to be placed in src Under the folder , Any path can , But there must be one under the project go.mod Folder , The production order is as follows :
go mod init Project name // go path turn go mod Execution can be Generate
// If there is a third-party package , It needs to be done again
go mod tidy // I will download the third-party package
边栏推荐
- Hands on deep learning (II) -- multi layer perceptron
- [live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!
- 蓝桥杯单片机省赛第八届
- go 分支与循环
- It took me only 3 months to jump out of the comfort zone and become an automated test engineer for 5 years
- Where can I buy cancer insurance? Which product is better?
- JVM knowledge points
- regular expression
- Welcome the winter vacation multi school league game 2 partial solution (B, C, D, F, G, H)
- go 包的使用
猜你喜欢
[live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!
Qt插件之Qt Designer插件实现
How should the team choose the feature branch development mode or trunk development mode?
Flutter中深入了解MaterialApp,常用属性解析
Nacos 配置中心整体设计原理分析(持久化,集群,信息同步)
[wireless image transmission] FPGA based simple wireless image transmission system Verilog development, matlab assisted verification
蓝桥杯单片机省赛第十二届第一场
The first practical project of software tester: web side (video tutorial + document + use case library)
0基础如何学习自动化测试?按照这7步一步一步来学习就成功了
The 7th Blue Bridge Cup single chip microcomputer provincial competition
随机推荐
PR zero foundation introductory guide note 2
Didi open source Delta: AI developers can easily train natural language models
u本位合约爆仓清算解决方案建议
蓝桥杯单片机省赛第十一届第二场
First acquaintance with string+ simple usage (II)
Three ways for programmers to learn PHP easily and put chaos out of order
潘多拉 IOT 开发板学习(RT-Thread)—— 实验1 LED 闪烁实验(学习笔记)
The 5th Blue Bridge Cup single chip microcomputer provincial competition
一天上手Aurora 8B/10B IP核(5)----从Framing接口的官方例程学起
SQL: common SQL commands
Basic syntax of unity script (7) - member variables and instantiation
Jetpack's livedata extension mediatorlivedata
Raspberry pie GPIO pin controls traffic light and buzzer
2022-07-01: at the annual meeting of a company, everyone is going to play a game of giving bonuses. There are a total of N employees. Each employee has construction points and trouble points. They nee
【DesignMode】建造者模式(Builder model)
NLog使用
[ibdfe] matlab simulation of frequency domain equalization based on ibdfe
Suggestions on settlement solution of u standard contract position explosion
Vite: configure IP access
go 语言命名规范