🐹 Go

POO: Clases y Objetos en Go (Golang): Una Guía Detallada

What is it and why? (a real-life analogy)

Imagine you decided to build a city from LEGO bricks. You have a box of parts, and you can assemble houses, cars, trees, and people from them. But what if, every time you needed a new house, you had to search the entire box for the right bricks, doors, and windows and assemble them from scratch? That would be very slow and inconvenient.

Now imagine you have a house blueprint. This blueprint shows how many floors the house has, what color the walls are, where the windows are, and what kind of roof it has. Now, to build a new house, you don't need to invent it from scratch every time — you just take the blueprint and build an exact copy according to it. You can build 10 or even 100 identical houses this way.

In programming, this blueprint is called a Class, and each individual house built from this blueprint is called an Object (or an instance of the class).

Important to remember: A class is a blueprint (template). An object is a specific thing created from that blueprint.

Let's expand the analogy. Every house has characteristics: address, number of residents, wall color. In programming, these are called fields or properties. And a house can also do things: open doors, turn on lights, start the heating. These are called methods.

Why is this needed? Imagine you are writing a game with 1000 enemies. If you describe each enemy separately (its health, speed, weapon), your program will turn into chaos. But if you create a class Enemy with fields health, speed and a method attack(), then you can create 1000 enemies with one command, and each will behave the same way but have its own health values.

Object-Oriented Programming (OOP) is an approach that helps programmers not get lost in complex programs. Instead of writing one huge block of code, we break the program into small, independent "bricks" — objects that communicate with each other.

Theory: how it works internally

Now let's look "under the hood" and understand how the computer processes classes and objects.

Memory and pointers

When you create a class, the computer does not allocate memory for it. A class is just an instruction, a description. Memory is allocated only when you create an object (instance).

Imagine computer memory is a huge warehouse with cells. A class is an instruction "take 5 adjacent cells: put the health number in the first, the name in the second, and the coordinates in the third, fourth, and fifth." When you create an object, the computer finds 5 free cells and fills them with data.

Go has an important feature: if you pass an object to a function, by default a copy of that object is passed. It's like taking a photo of your house and giving the photo to a friend — the friend can draw on the photo, but your real house won't change. To change the original, you need to pass a pointer to the object (the address of the house, not its photo).

Encapsulation

This smart word means "hiding the internal workings." Let's go back to the house. You don't go into the house and manually twist wires to turn on the light. You just flip the switch. Inside the house, there is complex electrical wiring, but you don't need to know about it.

In programming, it's the same: a good class hides its internal details and shows only what is needed for work. In Go, this is done using uppercase and lowercase letters in the names of fields and methods:

  • Uppercase letter (e.g., GetAge) — a public method, accessible to everyone.
  • Lowercase letter (e.g., calculateSalary) — a private method, accessible only within the same package.

Composition over inheritance

In classical OOP (like in Java or C++), there is the concept of "inheritance" — when one class can be an extension of another (e.g., the "Cat" class inherits from the "Animal" class). Go decided to take a different path.

Go uses composition — instead of saying "a car is a vehicle," we say "a car contains an engine, wheels, and a steering wheel." This is simpler and more flexible. If you need an object to be able to do something, you just add the necessary components to it.

Go's golden rule: "Prefer composition over inheritance." This makes code more flexible and understandable.

Syntax in Go (Golang): all forms and variations

Go does not have the class keyword. Instead, it uses structures (struct) and methods. Let's break down all the variations.

1. Declaring a structure (class)

// Declaring the "Student" structure
type Student struct {
    Name    string  // Student's name (public field)
    Age     int     // Age (public field)
    grade   float64 // Grade (private field, only within the package)
}

2. Creating an object (instance)

// Method 1: using a struct literal
student1 := Student{Name: "Anna", Age: 20, grade: 4.5}

// Method 2: using the new keyword (returns a pointer)
student2 := new(Student)
student2.Name = "Ivan"
student2.Age = 22

// Method 3: empty structure, then fill it
var student3 Student
student3.Name = "Maria"
student3.Age = 19

3. Methods (functions attached to a structure)

// Method with a "value receiver" (copy of the object)
func (s Student) GetInfo() string {
    return fmt.Sprintf("Student: %s, age: %d", s.Name, s.Age)
}

// Method with a "pointer receiver" (modifies the original)
func