Go packages and modules.
A package is a directory of .go files, and it is considered the basic block of a Go program. A module is a collection of packages.
Create the main module.
Execute the following commands.
$ mkdir myapp
$ cd myapp
$ go mod init myapp.org/application
go:creating new go.mod:module myapp.org/application
The file go.mod will be created; this file has the module’s definitions: name of the module, go version, dependencies, etc.
File go.mod:
module myapp.org/application
go 1.20
Create a package
To create a package you have to execute the following commands
$ cd myapp
$ mkdir entities
$ cd entities
Now, in the myapp/entities directory create the .go files. For example customers.go
package entities
type Customer struct {
Id int
Name string
Lastname string
CreditPoints float64
}
func NewCustomer(id int, name, lastname string, creditPoints float64) *Customer {
return &Customer{
Id: id,
Name: name,
Lastname: lastname,
CreditPoints: creditPoints,
}
}
By convention the package name should be the same as the directory name, in this case the directory name is entities so the package should be entities.
Note: It is important to consider that the elements in a packages must start with capital letters to be used and visible outside of the package (public element).
Using entities package
In the main.go file import the package entities using the name of our module (myapp.org/application) and the name of the package (entities).
package main
import (
"fmt"
"myapp.org/application/entities"
)
func main() {
customer:= entities.NewCustomer(1, "Fred", "Flintstone", 10.5)
fmt.Println("Id :", customer.Id)
fmt.Println("Name :", customer.Name)
fmt.Println("Lastnane :", customer.Lastname)
fmt.Println("Credit Points :", customer.CreditPoints)
}
Run the application.
$ cd myapp
$ go run .
Output:
Id : 1
Name : Fred
Lastnane : Flintstone
Credit Points : 10.5
Extra
We can create another file inside entities package with some functions.
Create a file called methods.go inside myapp/entities directory with the following code that will verify whether or not a customer has credit points.
package entities
func VerifyCreditPoints(c *Customer) bool {
if c.CreditPoints > 0 {
return true
} else {
return false
}
}
As we can see, the code belongs to the entities packages, we don’t need to import struct Customer, becase we are writing a function that is in the same scope / package (entities).
Now, modify the main.go file to add our new function that checks whether or not a customer has Credit Points.
package main
import (
"fmt"
"myapp.org/application/entities"
)
func main() {
customer:= entities.NewCustomer(1, "Fred", "Flintstone", 10.5)
fmt.Println("Id :", customer.Id)
fmt.Println("Name :", customer.Name)
fmt.Println("Lastnane :", customer.Lastname)
fmt.Println("Credit Points :", cusotmer.CreditPoints)
hasPoints := entities.VerifyCreditPoints(customer)
fmt.Printf("Customer %s has points? %v\n", customer.Name, hasPoints)
}
Run the application again.
$ cd myapp
$ go run .
Output:
Id : 1
Name : Fred
Lastnane : Flintstone
Credit Points : 10.5
Customer Fred has points? true