Think Golang in oop way
Tags: go
Published: 2024-05-24 09:53:52

---
title: Think Golang in oop way
published: true
description: 
tags: go,golang
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q52tbvcvsr5p2xrifpol.png
---

I began learning Golang for no reason other than it is a highly optimized, low CPU-consuming language that is excellent for creating microservices. It may appear humorous, but I began learning Go because of its adorable logo. 🥰 


Anyway, I started learning GoLlang with the intention of creating a highly effective and optimized backend API. I come from the `typescript` environment, where I could arrange my apps in multiple OOP paradigms, something I can't do in the functional world of Golang. I sink into a large body of water. I was looking through various backend libraries to learn how they organize their applications and how they configure/inherit functionality. All I discovered is that I must become a Go pointer master. Using this pointer, I can accomplish OOP capability in this language. That's why I started translating my OOP thinking into a Golang functional approach.

> This article is a Golang translation of OOP ideas.

### Basic Class property, method and it's object

First consider the below typescript code 👇 


```js
class Customer {
  public  identification: string;
  private name: string;
  private age: number;

  constructor(identification:string, name: string, age: number) {
    this.identification = identification;
    this.name = name;
    this.age = age;
  }

  getName() {
    return this.name;
  }

  getAge() {
    return this.age;
  }
}
```
#### Declare class and define fields

To translate this class into Golang, we need the struct of Go which is a composite type. we can define properties in a struct with visibility also. This is achieved by a simple rule: if a field starts with a lowercase letter, it's private and can only be accessed within the file it's declared. Conversely, if it begins with an uppercase letter, it's public and accessible from other files.

Here is the translated Customer class with it's public/private fields
```go
type Customer struct {
	Identification string
	name           string
	age            int
}
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bztuz6bj2nxrlggr0q3v.png)


## How do I create the object of this? 🥹

Cool down, Golang introduces a unique twist to object creation compared to other object-oriented languages. Instead of directly crafting objects like in `TypeScript`, Golang leans on a special convention: using a function named New() to initialize and return a pointer to the newly created struct. This function, starting with an uppercase letter, plays a crucial role in Golang's object instantiation. See the code below ⬇️ 

```go
func New(id string, name string, age int) *Customer {
	return &Customer{
		Identification: id,
		name:           name,
		age:            age,
	}
}
```

Create a Customer object by providing initial values to its constructor. In the following code snippet, only the capitalized fields are accessible, while private fields remain inaccessible.

```go
func main() {
	// Initializing a Customer object with constructor values
	customer := customer.New("customer1", "Rayhan", 27)

	// Accessing a public field
	fmt.Print(customer.Identification) //✅

	// Attempting to access private fields
	// This will result in an error
	fmt.Print(customer.name) //❌
	fmt.Print(customer.age)  //❌
}
```