Home Backend Development Golang What is a package in Go language

What is a package in Go language

Jan 11, 2023 am 10:19 AM
golang go language Bag

A package is a collection of multiple Go source codes and is an advanced code reuse solution. 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. Packages can be defined in very deep directories. The definition of the package name does not include the directory path, but the package is referenced. Generally use full path reference.

What is a package in Go language

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

The Go language uses packages to organize source code. A package is a collection of multiple Go source codes and is an advanced code reuse solution. Go language provides us with many built-in packages, such as fmt, os, io, etc.

Packages and folders in Golang have a one-to-one correspondence and must be created in the GOPATH directory before they can be used. If a package in Golang needs to reference the contents of another package, it must be imported using the import keyword before it can be used.

Any source code file must belong to a certain package, and the first line of valid code in the source code file must be the package pacakgeName statement, through which you declare the package you are in.

Basic concepts of packages

Go language packages use the organization form of a directory tree. Generally, the name of a package is 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, it is still recommended that the package name have 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 the c package, 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 GOPATH/src/github.com/userName /projectName directory.

  • 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.

Package import

To reference the contents of other packages in the code, you need to use the import keyword to import. package. The specific syntax is as follows:

import "包的路径"
Copy after login

Notes:

  • The import statement is usually placed below the package declaration statement at the beginning of the source code file;

  • The imported package name needs to be wrapped in double quotes;

  • The package name is calculated starting from GOPATH/src/, and uses / to separate the paths. .

Package import path

There are two ways to write the package reference path, namely full path import and relative Path import.

Full path import

The absolute path of the package is the storage path of the package behind GOROOT/src/ or GOPATH/src/, as shown below:

import "lab/test"
import "database/sql/driver"
import "database/sql"
Copy after login

The meaning of the above code is as follows:

  • The test package is a customized package, and its source code is located in the GOPATH/src/lab/test directory;

  • The source code of the driver package is located in the GOROOT/src/database/sql/driver directory;

  • The source code of the sql package is located in the GOROOT/src/database/sql directory.

Relative path import

Relative paths can only be used to import packages under GOPATH, and standard packages can only be imported using the full path.

For example, the path of package a is GOPATH/src/lab/a, and the path of package b is GOPATH/src/lab/b. If you import package a in package b, you can use the relative path to import it. Way. An example is as follows:

// 相对路径导入
import "../a"
Copy after login

Of course, you can also use the full path to import above, as shown below:

// 全路径导入
import "lab/a"
Copy after login

Package loading

Through the previous series of studies, I believe everyone has a general understanding of the startup and loading process of the Go program. Before executing the main function of the main package, the Go boot program will first initialize the entire program package. The entire execution process is shown in the figure below.

What is a package in Go language
Figure: Initialization of Go package

The initialization of Go language package has the following characteristics:

  • The package initialization program starts from the package referenced by the main function, searches for package references step by step, until it finds a package that does not reference other packages, and finally generates a directed acyclic graph of package references.

  • The Go compiler will convert the directed acyclic graph into a tree, and then initialize the package layer by layer starting from the leaf nodes of the tree.

  • The initialization process of a single package is as shown in the figure above. Constants are initialized first, then global variables, and finally the init function of the package is executed.

Golang package usage summary

Go language source code organization uses the form of packages. The main function of Go language can only be executed by the system in the main package.

【Related recommendations: Go video tutorial, Programming teaching

The above is the detailed content of What is a package in Go language. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1253
29
C# Tutorial
1228
24
How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? Apr 02, 2025 pm 04:54 PM

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

What is the problem with Queue thread in Go's crawler Colly? What is the problem with Queue thread in Go's crawler Colly? Apr 02, 2025 pm 02:09 PM

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

What libraries are used for floating point number operations in Go? What libraries are used for floating point number operations in Go? Apr 02, 2025 pm 02:06 PM

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

What should I do if the custom structure labels in GoLand are not displayed? What should I do if the custom structure labels in GoLand are not displayed? Apr 02, 2025 pm 05:09 PM

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

Which libraries in Go are developed by large companies or provided by well-known open source projects? Which libraries in Go are developed by large companies or provided by well-known open source projects? Apr 02, 2025 pm 04:12 PM

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

In Go programming, how to correctly manage the connection and release resources between Mysql and Redis? In Go programming, how to correctly manage the connection and release resources between Mysql and Redis? Apr 02, 2025 pm 05:03 PM

Resource management in Go programming: Mysql and Redis connect and release in learning how to correctly manage resources, especially with databases and caches...

How to solve the problem of Golang generic function type constraints being automatically deleted in VSCode? How to solve the problem of Golang generic function type constraints being automatically deleted in VSCode? Apr 02, 2025 pm 02:15 PM

Automatic deletion of Golang generic function type constraints in VSCode Users may encounter a strange problem when writing Golang code using VSCode. when...

Golang's Purpose: Building Efficient and Scalable Systems Golang's Purpose: Building Efficient and Scalable Systems Apr 09, 2025 pm 05:17 PM

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.

See all articles