Packages in Go
A package in Go is a way to organize code and promote reusability. Every Go program is made up of at least one package, and the main
package is required to create an executable.
Keep in mind
Each directory in a Go project should have the same package name.
Declaring and Using Packages
Declaring a Package
Every Go file starts with a package
declaration:
Importing Packages
Use the import
keyword to include packages:
For multiple imports:
Creating and Using Custom Packages
Create a Package
- Create a new directory (e.g.,
mypackage
). - Add a
.go
file with the package name:
Use the Custom Package
-
In the main program, import your package:
-
Run the program:
Warning
The below command will not work, as all the executables have not been mentioned.
You can either use:go run main.go mypkg/mypkg.go
or better go run .
Package Visibility: Exported vs. Unexported
- Exported identifiers (functions, variables, constants, types) start with an uppercase letter.
- Example:
SayHello
(accessible outside the package). - Unexported identifiers start with a lowercase letter.
- Example:
helperFunc
(accessible only within the same package).
Example
// mypackage/mypackage.go
package mypackage
import "fmt"
// Exported
func SayHello(name string) {
fmt.Printf("Hello, %s!\n", name)
}
// Unexported
func helperFunc() {
fmt.Println("This is a helper function")
}
The function helperFunc
cannot be used outside mypackage
.
Package Initialization
You can define an init
function in a package. This function runs automatically when the package is imported.
Example
When the package is imported, the init
function runs before any other code in the package.
Go Modules and Packages
Go modules make package management simple: 1. Initialize a module:
2. Go automatically manages dependencies when you import third-party packages.Example with Third-Party Packages
Install a package (e.g., github.com/spf13/cobra
):
Import and use it in your code:
Best Practices
-
Keep Packages Cohesive: Each package should have a single responsibility.
-
Avoid Circular Dependencies: Packages should not depend on each other in a circular manner.
-
Use
internal
for Private Packages: - The
internal
directory restricts the use of its packages to the parent module. - Example:
-
The
helper
package can only be used by code inside theproject
module. -
Document Your Package: Add comments to exported functions, constants, and types. This helps tools like
godoc
generate documentation.
Common Go Standard Library Packages
Package | Description |
---|---|
fmt |
Formatting input/output. |
os |
OS-level functionality (files, environment vars). |
time |
Time and date manipulation. |
net/http |
HTTP client and server. |
io |
Input/output primitives. |
math |
Mathematical functions and constants. |
sync |
Synchronization primitives like mutexes. |
context |
Managing request contexts (e.g., cancellations). |