DEV Community

Cover image for Think Golang in oop way
King Rayhan
King Rayhan

Posted on

Think Golang in oop way

#go

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 👇

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;
  }
}
Enter fullscreen mode Exit fullscreen mode

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

type Customer struct {
    Identification string
    name           string
    age            int
}
Enter fullscreen mode Exit fullscreen mode

Image description

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 ⬇️

func New(id string, name string, age int) *Customer {
    return &Customer{
        Identification: id,
        name:           name,
        age:            age,
    }
}
Enter fullscreen mode Exit fullscreen mode

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.

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)  //❌
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)