Home > Backend Development > Golang > Why does Go language organize code in packages?

Why does Go language organize code in packages?

青灯夜游
Release: 2023-01-05 10:16:30
Original
3918 people have browsed it

Reason: It is unreasonable to put all the code of the program into one source code file. Related codes need to be managed in separate files. However, as the number of program files increases, there must be a way to organize and manage the files. / form, so Go introduced the concept of "package". Package is a logical organizational form for "decentralized management" and "unified use" of program functions/attributes.

Why does Go language organize code in packages?

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

Package Introduction

The program files we develop using the go language are called source code files (source code files must end with .go). Obviously, it is unreasonable to put all the code of the program into one source code file. The relevant code needs to be managed in separate files. However, as the number of program files increases, there must be a way/form to organize and manage the files, so Go introduced Understand the concept of "package".

Package is a virtual concept provided by the Go language. Multiple source code files with consistent package declarations are logically organized together and belong to the same package.

Go language packages use the organizational form of a directory tree. Generally, the name of a package is the name of the directory where its source file is located. Although the Go language does not mandate that the package name must have the same name as the directory name where it is located. , but it is still recommended that the package name has the same name as the directory where it is located, so that the structure is clearer.

Packages can be defined in very deep directories. The definition of the package name does not include the directory path, but the full path is generally used when referencing the package. For example, define a package c under GOPATH/src/a/b/. In the source code of package c, you only need to declare it as package c instead of package a/b/c. However, when importing package c, you need to bring the path, such as import "a/b/c".

Idiomatic usage of packages:

  • Package names are generally lowercase, use a short and meaningful name.

  • The package name generally has the same name as the directory where it is located, or it can be different. The package name cannot contain special symbols such as -.

  • Packages generally use the domain name as the directory name, which ensures the uniqueness of the package name. For example, the packages of GitHub projects are generally placed in the GOPATH/src/github.com/userName/projectName directory. Down.

  • The package named main is the entry package of the application. When compiling the source code file that does not contain the main package, you will not get an executable file.

  • All source code files in a folder can only belong to the same package. Source code files that also belong to the same package cannot be placed in multiple folders.

(1) Package declaration, package import path, and issues to note

//一:包的声明
// 1、每个源码文件都必须在文件头处声明自己归属的包。
package 包名 // 包名一般总是用小写字母

// 2、包名任意,包名一致的属于同一个包

// 3、包是编译和归档Go程序的最基本单位,一个包中的多个源码文件是一个不可分割的整体

//二:包的导入路径
强调强调强调!!!!!!
包是一个把多个源码文件归一到一起管理的虚拟单位,一定要记住,它只是一个虚拟的概念而已,而实实在在地讲,多个源码文件是要放置到一个实实在在的文件夹下的,这个实实在在的文件夹所处的路径是包的导入路径。包的导入路径很重要,他是包的"家庭住址",是用来找到包的(用在import语句中,稍后介绍),但它绝不等同于包的概念

//三:注意的问题
1、一个文件夹下只能放置一个包,也就是所一个文件夹下放置的多个源码文件的包声明必须一致,go以此来确保一个路径就唯一定位到唯一的一个包。
2、包虽然与文件夹路径是截然不同的意思,但是为了方便记忆,包通常应该声明为文件夹的名字
例如文件夹路径/a/b/c/mypkg,包名应声明为package mypkg,mypkg就为包名
Copy after login

Why does Go language organize code in packages?

(2) Differentiation and placement of packages

Package is a virtual and logical concept, but the multiple source code files organized by the package are indeed real , must be placed in a certain folder.

Please note: For the sake of simplicity in subsequent writing, the author directly refers to the storage location of multiple source code files organized by the package as the storage location of the package. Readers must be aware of this. .

The main package contains the entry point of the program and is mainly used to run it. In any case, the main package can be placed in any folder.

The author calls packages other than the main package other packages, specifically referring to built-in packages, custom packages, and downloaded third-party packages. Different from the main package, other packages are mainly used to be imported and used. The placement locations are as follows

// 内置包
内置包固定被放置在`$GOROOT/src/`下,与任何模式无关

// 自定义包
在未启用modules模式的情况下,自定义包需要放置在GOPATH指定的任意目录下的src中

// 下载的第三方包
在未启用modules模式的情况下,使用go工具链命令下载的第三方包总是默认被存放到GOPATH的第一个目录的src下 


// 强调一下
在早期的Go环境中,自定义的包与下载的第三方包都是放到了$GOPATH/src下,因为早期Go采用的是和GOPATH模式,而且即便是在GO1.14.2版本中,在我们还未学习如何使用任何新模式前,默认使用的仍是GOPATH模式
Copy after login

ps:

1. The built-in package is the author’s pet name for the standard package

2. Any package stored in the GOPATH workspace is officially called a workspace package

(3) Use of packages

// 1、一个源码文件中声明的函数、类型、变量和常量等标识符/名字对同一包中的所有其他源码文件都可见,不需要加任何前缀即可引用,因为代码包只是一种组织管理源码文件的形式,同一个包下的多个源码文件本就属于一个整体,事实上我们完全可以将一个包当成一个”大文件“去看,毫无疑问这个”大文件“中声明的标识符/名字不能重名

// 2、包名很关键
名为main的包可简称为”main包“,是程序的入口,go程序运行时会查找main包下的main函数,main函数既没有参数声明也没有结果声明,见下图

名不为main的包可称之为”其他包“,是程序的功能/属性集合,此类包用来被其他包导入使用,为何此时包名仍然很关键呢?因为我们导入时用的是导入路径(指的是包所在的路径,该路径不是绝对路径,稍后介绍),但是使用的则可能会使用"包名.xxx"
Copy after login

Why does Go language organize code in packages?

To sum up: Package is a logical organizational form for "decentralized management" and "unified use" of program functions/attributes

[Related recommendations: Go video tutorialprogramming teaching

The above is the detailed content of Why does Go language organize code in packages?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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