Introduction
Packages in Go are a way to organize and reuse code. They allow you to group related functions, types, and variables together, making your code more modular and easier to maintain. The Go standard library itself is made up of packages, providing a rich set of functionalities that you can use in your programs. In this chapter, you will learn the basics of creating and using packages in Go, with examples to illustrate their usage.
Creating a Package
To create a package, you need to create a directory for the package and write your Go code in one or more .go
files within that directory. Each file should start with a package
declaration.
Example: Creating a Simple Package
Directory Structure:
myapp/
main.go
mathutils/
mathutils.go
mathutils/mathutils.go:
package mathutils
// Add adds two integers and returns the result.
func Add(a, b int) int {
return a + b
}
// Multiply multiplies two integers and returns the result.
func Multiply(a, b int) int {
return a * b
}
In this example, we created a package named mathutils
with two functions: Add
and Multiply
.
Using a Package
To use a package in your Go program, you need to import it using the import
statement, specifying the package path relative to your GOPATH
or module root.
main.go:
package main
import (
"fmt"
"myapp/mathutils"
)
func main() {
sum := mathutils.Add(3, 4)
product := mathutils.Multiply(3, 4)
fmt.Println("Sum:", sum) // Output: Sum: 7
fmt.Println("Product:", product) // Output: Product: 12
}
In this example, we imported the mathutils
package and used its Add
and Multiply
functions in the main
function.
Exporting and Unexporting Identifiers
In Go, an identifier (such as a function, type, or variable) is exported if it starts with an uppercase letter. Exported identifiers can be accessed from other packages, while unexported identifiers (starting with a lowercase letter) cannot.
Example:
package mathutils
// Add is an exported function.
func Add(a, b int) int {
return a + b
}
// multiply is an unexported function.
func multiply(a, b int) int {
return a * b
}
In this example, Add
is an exported function, and multiply
is an unexported function. Only Add
can be accessed from other packages.
Organizing Packages
Standard Library
Go comes with a rich standard library that is organized into packages. You can use these packages by importing them in your code.
Example:
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.ToUpper("hello, world")) // Output: HELLO, WORLD
}
Third-Party Packages
You can also use third-party packages by downloading them using the go get
command and importing them into your code.
Example:
go get github.com/gorilla/mux
Using the Package:
package main
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
)
func main() {
r := mux.NewRouter()
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, World!")
})
http.Handle("/", r)
http.ListenAndServe(":8080", nil)
}
Conclusion
Packages in Go are essential for organizing and reusing code. By understanding how to create, use, and manage packages, you can write more modular, maintainable, and efficient Go programs. Packages allow you to encapsulate functionality, making your code easier to understand and use. Whether you are using standard library packages, third-party packages, or creating your own, mastering packages is crucial for effective Go programming.