Go Packages: Difference between revisions
Line 6: | Line 6: | ||
A Go package is a collection of [[Go_Language#Variables|variables]], [[Go_Functions|functions]] and [[Go_Language#Type|type]] definitions, such as [[Go_Structs#Overview|structs]], and [[Go_Interfaces#Override|interfaces]] that are inter-related and provide coherent, unified functionality. The intention behind bundling together such features into package is to make the design and the maintenance of large programs practical. Packages are units that can be easier understood and changed, and that can also evolve independently of other packages. These characteristics allow packages to be shared, distributed and reused by different and otherwise independent projects. | A Go package is a collection of [[Go_Language#Variables|variables]], [[Go_Functions|functions]] and [[Go_Language#Type|type]] definitions, such as [[Go_Structs#Overview|structs]], and [[Go_Interfaces#Override|interfaces]] that are inter-related and provide coherent, unified functionality. The intention behind bundling together such features into package is to make the design and the maintenance of large programs practical. Packages are units that can be easier understood and changed, and that can also evolve independently of other packages. These characteristics allow packages to be shared, distributed and reused by different and otherwise independent projects. | ||
Packages provide a [[Go_Packages#Packages_as_Namespaces|namespace]] for their names | Packages provide a [[Go_Packages#Packages_as_Namespaces|namespace]] for their names. | ||
They also provide an [[Go_Packages#Packages_as_Encapsulation_Mechanism|encapsulation mechanism]] for their code, by hiding implementation details and only exposing features, such as [[Go_Language#Variables|variables]], [[Go_Language#Type|types]] or [[Go_Functions|functions]] that are meant to be publicly consumed. | |||
Package are declared by writing multiple code files annotated with the same <code>[[Go_Language#package_keyword|package]]</code> name, and are consumed by other packages by importing them with the <code>[[Go_Language#import_keyword|import]]</code> keyword. | Package are declared by writing multiple code files annotated with the same <code>[[Go_Language#package_keyword|package]]</code> name, and are consumed by other packages by importing them with the <code>[[Go_Language#import_keyword|import]]</code> keyword. |
Revision as of 00:22, 7 September 2023
Internal
Overview
Go modularization is build upon the concept of package.
A Go package is a collection of variables, functions and type definitions, such as structs, and interfaces that are inter-related and provide coherent, unified functionality. The intention behind bundling together such features into package is to make the design and the maintenance of large programs practical. Packages are units that can be easier understood and changed, and that can also evolve independently of other packages. These characteristics allow packages to be shared, distributed and reused by different and otherwise independent projects.
Packages provide a namespace for their names.
They also provide an encapsulation mechanism for their code, by hiding implementation details and only exposing features, such as variables, types or functions that are meant to be publicly consumed.
Package are declared by writing multiple code files annotated with the same package
name, and are consumed by other packages by importing them with the import
keyword.
Packages as Namespaces
Each package defines a distinct namespace that encloses all its identifiers. When a public feature of a package, such as a function or a type is used outside of the package, its name is prefixed with the name of the package, make it the combination, named ..., unique in the universe block.