Golang Object-Oriented Programming

Everything you wanted to know about structs in Golang

Mayank Gupta
Nov 22, 2019 · 6 min read

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

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 Name, Age, Designation, and 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.

https://gist.github.com/Mayankgupta688/a15e74ed06a344b9607fa9687fe5a7d6

The above code contains the following building blocks:

  1. The type keyword can be used to define the new type in Golang.
  2. Employee has been passed as a new struct name.
  3. The struct keyword notifies us that we’re creating a struct data type.
  4. 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

Passing comma-separated values to the struct

https://gist.github.com/Mayankgupta688/dd20ed5294b0ee262e42bc5e9da519b8

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 newEmployee variable.

Problems with the above approach:

  1. We need to remember all the properties and their order while creating the object.
  2. All of the values need to be passed to the struct.

Passing key-value pairs

  1. No need to add values in the same order.
  2. No need to specify all the key-value pairs.
https://gist.github.com/Mayankgupta688/673b7e8b1bbf72273ce204973f54d241

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

  • The int value is assigned with the value 0.
  • The string values are assigned with “” (empty string).
  • The bool values are assigned with the default value, false.

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 struct object.

https://gist.github.com/Mayankgupta688/fd546fbb214e8c36e64f3f68415b9384

Object reference or object value?

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

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.

https://gist.github.com/Mayankgupta688/e8e810149085c4c12d999d969c3a8ff3

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

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.

  1. The default value of a Boolean type is false.
  2. The default value for the integer type is 0.
  3. The default value for the string type is “” (empty string).

Let's try creating some dummy code around the concept for better clarity:

https://gist.github.com/Mayankgupta688/06b499f62936c0324b26192f85d35b6f

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 Employee object.

Passing an object to a function

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

Let's see this with the help of an example:

https://gist.github.com/Mayankgupta688/9f5bc05b1bc13d71491e3217535a8e81

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 emp.

Let's see next how to invoke the function added to the struct using Golang.

https://gist.github.com/Mayankgupta688/ef27988aad0478e5a915a373c77757b1

TechnoFunnel

TechnoFunnel focus on learning and understanding the programming concepts

Mayank Gupta

Written by

9 Years of Experience with Front-end Technologies and MEAN Stack. Working on all Major UI Frameworks like React, Angular and Vue https://medium.com/technofunnel

TechnoFunnel

We are focussed towards learning different technologies like React, Angular, Machine Learning, Python, Golang and AWS. Follow us for detailed conceptual understanding of these technologies and Interview Preparations.

Mayank Gupta

Written by

9 Years of Experience with Front-end Technologies and MEAN Stack. Working on all Major UI Frameworks like React, Angular and Vue https://medium.com/technofunnel

TechnoFunnel

We are focussed towards learning different technologies like React, Angular, Machine Learning, Python, Golang and AWS. Follow us for detailed conceptual understanding of these technologies and Interview Preparations.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store