Go Packages: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 15: Line 15:
Each package defines a distinct namespace that hosts all its [[Go_Language#Identifiers_.28Names.29|identifiers]]. Within the package namespace, all identifiers must be unique.
Each package defines a distinct namespace that hosts all its [[Go_Language#Identifiers_.28Names.29|identifiers]]. Within the package namespace, all identifiers must be unique.


When a public feature of a package, such as a function or a type is used outside of the package, its name must prefixed with the name of the package, with the intention of making the package and feature name pair unique in the [[Go_Language#Universe_Block|universe block]]. Such a name is referred to as '''qualified identifier'''. For example, the <code>Println()</code> function, exported by the <code>fmt</code> package, is invoked with the <code>fmt.Printf(...)</code> qualified identifier:
When a public feature of a package, such as a function or a type is used outside of the package, its name must prefixed with the name of the package, with the intention of making the package and feature name pair unique in the [[Go_Language#Universe_Block|universe block]]. Such a name is referred to as '''qualified identifier'''. For example, the <code>Println()</code> function, exported by the <code>fmt</code> package, is invoked with the <code>fmt.Printf</code> qualified identifier:
<syntaxhighlight lang='go'>
<syntaxhighlight lang='go'>
import "fmt"
import "fmt"

Revision as of 00:33, 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 members.

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 one or more source 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 hosts all its identifiers. Within the package namespace, all identifiers must be unique.

When a public feature of a package, such as a function or a type is used outside of the package, its name must prefixed with the name of the package, with the intention of making the package and feature name pair unique in the universe block. Such a name is referred to as qualified identifier. For example, the Println() function, exported by the fmt package, is invoked with the fmt.Printf qualified identifier:

import "fmt"
...
fmt.Println("hello")

Packages as Encapsulation Mechanism

Declaring Packages

Publishing Packages

Consuming Packages

TO DO