Structs Instead of Classes
In this tutorial, we are going to discuss on how to use structs instead of classes in Go language. Go language is not a pure object oriented programming language.
Go language does not provide classes but it does provide structs. Methods can be added on structs. This provides the behavior of bundling the data and methods that operate on the data together similar to a class.
Let’s start with an example right away for a better understanding. We will create a custom package in this example as it helps to better understand how structs can be an effective replacement for classes.
Example
Create a sub folder inside ~/Documents/
and name it oop
. Let’s initialize a go module named oop
. Type the following command inside the oop
directory to create a go mod named oop
.
$ mkdir oop
$ cd oop
$ go mod init oop
After running the above command go will create a file called go.mod
and contents of this file is
module oop
go 1.15
Now create a sub folder employee
inside oop
. Inside the employee
folder, create a file named employee.go
. The folder structure would look like,
βββ Documents
β βββ oop
β βββ employee
β β βββ employee.go
β βββ go.mod
Now replace the contents of employee.go
with the following Go code
package employee
import "fmt"
type Employee struct {
FirstName string
LastName string
TotalLeaves int
LeavesTaken int
}
func (e Employee) LeavesRemaining() {
fmt.Printf("%s %s has %d leaves remaining\n", e.FirstName, e.LastName, (e.TotalLeaves - e.LeavesTaken))
}
In the above program, line no. 1 specifies that this file belongs to the employee
package. An Employee struct is declared in line no. 5.
A method named LeavesRemaining
is added to the Employee
struct in line no. 12. This method calculates and displays the number of remaining leaves an employee has.
Now we have a struct and a method that operates on a struct bundled together similar to a class.
So finally create a file named main.go
inside the oop
folder. Now the folder structure would look like,
βββ Documents
β βββ oop
β βββ employee
β β βββ employee.go
β βββ go.mod
β βββ main.go
Now replace the contents of employee.go
with the following Go code
package main
import "oop/employee"
func main() {
emp := employee.Employee{
FirstName: "Ashok Kumar",
LastName: "Mariyala",
TotalLeaves: 18,
LeavesTaken: 2,
}
emp.LeavesRemaining()
}
Output
$ go run main.go
Ashok Kumar Mariyala has 16 leaves remaining
In the above program, we imported the employee
package in line no. 3 and we initialized Employee
structure in line no.6. The LeavesRemaining()
method of the Employee
struct is called from line no. 12 in main()
.
This program cannot be run on the playground as it has a custom package. If you run this program in your local by issuing the above command (go run main.go).
New() function instead of constructors
The program we wrote above looks alright but there is a subtle issue in it.
Let’s see what happens when we define the employee struct with zero values. Replace the contents of main.go
with the following code,
package main
import "oop/employee"
func main() {
var e employee.Employee
e.LeavesRemaining()
}
The only change we have made is creating a zero value Employee
in line no. 6 (Not initialized structure). This program will output,
has 0 leaves remaining
As you can see, the variable created with the zero value of Employee
is unusable. It doesn’t have a valid first name, last name and also doesn’t have valid leave details.
In other OOP languages like java, this problem can be solved by using constructors. A valid object can be created by using a parameterized constructor.
Go doesn’t support constructors. If the zero value of a type is not usable, it is the job of the programmer to unexport the type to prevent access from other packages and also to provide a function named NewT(parameters)
which initializes the type T
with the required values.
It is a convention in Go to name a function that creates a value of type T
to NewT(parameters)
. This will act as a constructor.
If the package defines only one type, then it’s a convention in Go to name this function just New(parameters)
instead of NewT(parameters)
.
Let’s make changes to the program we wrote so that every time an employee is created, it is usable.
Example
package employee
import (
"fmt"
)
type employee struct {
firstName string
lastName string
totalLeaves int
leavesTaken int
}
func New(firstName string, lastName string, totalLeave int, leavesTaken int) employee {
e := employee{firstName, lastName, totalLeave, leavesTaken}
return e
}
func (e employee) LeavesRemaining() {
fmt.Printf("%s %s has %d leaves remaining\n", e.firstName, e.lastName, (e.totalLeaves - e.leavesTaken))
}
We have made some important changes here. By doing lower case letters, we have successfully unexported the employee
struct and prevented access from other packages.
It’s a good practice to make all fields of an unexported struct to be unexported too unless there is a specific need to export them.
Since we don’t need to access the fields of the employee
struct anywhere outside the employee
package, we have unexported all the fields too.
Now since employee
is unexported, it’s not possible to create values of type Employee
from other packages.
Hence we are providing an exported New
function which takes the required parameters as input and returns a newly created employee.
Now replace the contents of main.go
with the following,
package main
import "oop/employee"
func main() {
e := employee.New("Ashok Kumar", "Mariyala", 18, 2)
e.LeavesRemaining()
}
Output
$ go run main.go
Ashok Kumar Mariyala has 16 leaves remaining
Thus you can understand that although Go doesn’t support classes, structs can effectively be used instead of classes and methods of signature New(parameters)
can be used in the place of constructors.