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.
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就为包名
(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模式
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"
To sum up: Package is a logical organizational form for "decentralized management" and "unified use" of program functions/attributes
[Related recommendations: Go video tutorial、programming 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!

Goimpactsdevelopmentpositivelythroughspeed,efficiency,andsimplicity.1)Speed:Gocompilesquicklyandrunsefficiently,idealforlargeprojects.2)Efficiency:Itscomprehensivestandardlibraryreducesexternaldependencies,enhancingdevelopmentefficiency.3)Simplicity:

C is more suitable for scenarios where direct control of hardware resources and high performance optimization is required, while Golang is more suitable for scenarios where rapid development and high concurrency processing are required. 1.C's advantage lies in its close to hardware characteristics and high optimization capabilities, which are suitable for high-performance needs such as game development. 2.Golang's advantage lies in its concise syntax and natural concurrency support, which is suitable for high concurrency service development.

Golang excels in practical applications and is known for its simplicity, efficiency and concurrency. 1) Concurrent programming is implemented through Goroutines and Channels, 2) Flexible code is written using interfaces and polymorphisms, 3) Simplify network programming with net/http packages, 4) Build efficient concurrent crawlers, 5) Debugging and optimizing through tools and best practices.

The core features of Go include garbage collection, static linking and concurrency support. 1. The concurrency model of Go language realizes efficient concurrent programming through goroutine and channel. 2. Interfaces and polymorphisms are implemented through interface methods, so that different types can be processed in a unified manner. 3. The basic usage demonstrates the efficiency of function definition and call. 4. In advanced usage, slices provide powerful functions of dynamic resizing. 5. Common errors such as race conditions can be detected and resolved through getest-race. 6. Performance optimization Reuse objects through sync.Pool to reduce garbage collection pressure.

Go language performs well in building efficient and scalable systems. Its advantages include: 1. High performance: compiled into machine code, fast running speed; 2. Concurrent programming: simplify multitasking through goroutines and channels; 3. Simplicity: concise syntax, reducing learning and maintenance costs; 4. Cross-platform: supports cross-platform compilation, easy deployment.

Confused about the sorting of SQL query results. In the process of learning SQL, you often encounter some confusing problems. Recently, the author is reading "MICK-SQL Basics"...

The relationship between technology stack convergence and technology selection In software development, the selection and management of technology stacks are a very critical issue. Recently, some readers have proposed...

Golang ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.