Golang Object-Oriented Programming
This article is focused on working with object-oriented architecture in Golang. We don’t have classes in Golang, so the only way to support the object-oriented model is to work with structs.
Creating Structs in Golang
Structs can be used to represent a complex object comprising multiple key-value pairs.
Let's look for a scenario: We want to represent an employee in our organization. In order to represent an employee, we require a combination of key-value pairs containing all of the the employee-related data. The employee entity can be comprised of multiple keys/properties, such as
Salary. All of these attributes/properties collectively represent a single employee in our organization.
Let's create a simple
Employee dtruct with basic data properties.
The above code contains the following building blocks:
typekeyword can be used to define the new
Employeehas been passed as a new struct name.
structkeyword notifies us that we’re creating a struct data type.
- Adding properties to the struct along with the property type.
Golang ensures strict type safety; therefore, we need to specify the property type during the
struct declaration. Assigning the value of any other type in these properties will result in compile-time errors.
Creating Objects From Struct
Once the new struct is available, we then require ways to create objects out of it. Next, we’ll look into the details of how to create an object for the defined struct (
Employee). There are multiple ways to create an object — we’ll look into the different possible scenario along with their advantages and disadvantages.
Passing comma-separated values to the struct
The simplest way to create an object is to pass all the values needed by the struct as comma-separated-values. The values specified should be in the same order as specified during the struct declaration.
In the above code, we’re creating a struct object by providing all the values as comma-separated values to the
Employee struct. A new object is created and assigned to the
Problems with the above approach:
- We need to remember all the properties and their order while creating the object.
- All of the values need to be passed to the struct.
Passing key-value pairs
To overcome the downside of the above approach, we can create an object by passing key-value pairs. Passing the keys during declaration offers the following differences:
- No need to add values in the same order.
- No need to specify all the key-value pairs.
In the above declarations, we can see that we can pass key-value pairs in any order, and also we can skip some parameters. The struct is still created with missing values and a random order.
Default values for missing properties
In the above case, while creating the
newEmployee object, we’re omitting one of the key-values (
Salary). By default, Golang adds a default value to the property
Salary. The default value is assigned according to the property type. The following default data values are assigned according to property types.
intvalue is assigned with the value
stringvalues are assigned with
boolvalues are assigned with the default value,
So in the above case, since the property is of type
int, the default value of
Salary would be
0. Let's see next, how to access these property values using the
Object reference or object value?
Another important aspect of the creation of a
struct with the approach mentioned above is to determine whether the property
newEmployee represents a value type or a reference type. The object returned from the above declaration returns us the object value and not the references.
newEmployee doesn’t point to any memory location.
If this object is passed as a parameter to some other function, we’re providing the object’s values to the called function. The original object is copied, and a new data object is assigned to the caller-function parameter. The object isn’t passed as a reference. Since the object isn’t passed as a reference, any changes made to the copied object aren’t replicated in the original object. Let's see the above scenario with an example:
In the above example, even when we’re updating the values in the
UpdateEmployee function, the original object
newEmployee isn’t impacted since we’ve passed the object as a value and not a reference.
Passing an object as a reference
In the above case, since we didn’t pass the reference to the function, the update to the object in the function won’t be reflected. In order to pass the object as a reference, we can pass the object reference instead of the values.
We can send the object as a reference by appending
& in front of it, which represents the address-of operator. The called function needs to be updated as well to accept the reference rather than the values.
In the above code, the function (as well as the function call) has been updated to send and receive the memory location instead of the value for the ibject. Now if the data is updated in the called function, then that’ll be replicated in the original data object.
The problem in this approach is that we have to explicitly use the address-of operator to extract the address of the object and send it to the function.
Using the New Keyword to Create Objects
Another way to create an object from a struct is to use a
new keyword. While using a
new keyword, Golang creates a new object of the type
struct and returns its memory location back to the variable. In short, the
new keyword returns the address of the object.
Using the reference returned from the
new keyword, we can assign values to the properties of the new object created. All the properties, by default, take the implicit values that are defined as a default value for the specific type.
- The default value of a Boolean type is
- The default value for the integer type is
- The default value for the string type is
Let's try creating some dummy code around the concept for better clarity:
In the above case, we’re creating a new object using the
new keyword. Since we’re using the
new keyword, we can’t provide the default values to the object properties. When the object is created, Golang provides the default values to the object properties. In the above case, the access to the
Name property will return an empty string (
The above code is now returning the address of the new object created using the
new keyword. The variable
newEmployee is a pointer to the
Passing an object to a function
When we’re creating an object with the
new keyword, the address of the object is returned. So if we’re sending the object as a parameter to the function, the reference of the object is sent. Any changes made to the input object parameter will be reflected in the original object.
In the above case, the object reference is returned from the
new keyword. Therefore, while invoking the function, we don’t need to use
& to send the reference of the object.
Adding a Function to the Struct
The struct not only defines the properties associated with the object but also represents the behavior of the object. We can add functions to the struct that can add behavior to it. The code to associate a function to the struct is pretty different in Golang.
Let's see this with the help of an example:
In the above code, we’re adding a new function that’s bound to the
Employee struct. We need to explicitly bind the function to the struct. This function defined can then take the reference of the object created using
Let's see next how to invoke the function added to the struct using Golang.
I hope you liked the article.
Refer to the following pieces for more information on Golang: