Golang Object-Oriented Programming

Everything you wanted to know about structs in Golang

Mayank Gupta
Published in
6 min readNov 22, 2019


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

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

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



Mayank Gupta

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