background-shape
feature-image

Go language is a statically typed language that supports the principles of Object-Oriented Programming (OOP). Go’s OOP implementation is a bit different from traditional OOP languages like Java, Python, and Ruby, as it does not have classes. Instead, it uses a type-based inheritance mechanism, called “struct embedding”, to achieve inheritance-like behavior.

In Go, a struct is a composite data type that can have fields, methods, and other properties. The fields of a struct represent the state of an object, while the methods of a struct represent the behavior of an object.

Here is a basic example of a struct and its methods in Go:

1
2
3
4
5
6
7
type Person struct {
  Name string
  Age  int
}
func (p *Person) Greet() {
  fmt.Println("Hello, my name is", p.Name)
}

In the above example, we define a struct Person with two fields, Name and Age. The struct also has a method Greet, which prints a greeting message.

To create an object from a struct, you can use the new keyword or the short-hand notation:

1
2
3
4
5
6
p1 := new(Person)
p2 := &Person{Name: "Alice", Age: 30}
To call a method on an object, you use the dot notation:

p1.Greet()
p2.Greet()

This is a simple example of how you can implement OOP concepts in Go using structs and methods. Let’s see how you can implement inheritance in Go using struct embedding.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
type Person struct {
  Name string
  Age  int
}

func (p *Person) Greet() {
  fmt.Println("Hello, my name is", p.Name)
}

type Student struct {
  Person
  School string
}

func (s *Student) Study() {
  fmt.Println(s.Name, "is studying at", s.School)
}

In the above example, we define a new struct Student that embeds the Person struct. This means that the Student struct will have all the fields and methods of the Person struct. We can then add additional fields and methods to the Student struct to represent its unique properties and behavior.

To create an object of the Student type, you can use the following code:

1
2
3
4
5
6
7
s := &Student{
  Person: Person{
    Name: "Bob",
    Age: 20,
  },
  School: "Go University",
}

With this code, you can call both the Greet method from the Person struct and the Study method from the Student struct on the s object:

1
2
s.Greet()
s.Study()

This is a simple example of how you can implement inheritance-like behavior in Go using struct embedding. The concept can be extended to create more complex OOP hierarchies as needed.

I hope this gives you a good idea of how OOP is implemented in Go, and how you can use structs and methods to create objects with state and behavior.

In addition to structs and methods, Go also supports several other OOP concepts, including:

  1. Polymorphism:

Go supports polymorphism through the use of interfaces. An interface defines a set of methods that can be implemented by any type. A type that implements an interface is said to be polymorphic with respect to that interface.

Here’s an example of how to define and implement an interface in Go:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
type Shape interface {
  Area() float64
  Perimeter() float64
}

type Rectangle struct {
  width, height float64
}

func (r Rectangle) Area() float64 {
  return r.width * r.height
}

func (r Rectangle) Perimeter() float64 {
  return 2 * (r.width + r.height)
}

In this example, we define an interface Shape with two methods, Area and Perimeter. We then define a struct Rectangle that implements this interface by providing implementations for both methods.

  1. Encapsulation: Go supports encapsulation through the use of private fields. A field that is declared as private (by starting its name with a lowercase letter) is not accessible from outside the struct. This allows you to hide implementation details and control access to the internal state of objects.

  2. Abstraction: Go supports abstraction through the use of interfaces and methods. An interface defines a contract that must be fulfilled by any type that implements it, allowing you to write code that works with objects of multiple types, without having to know the details of their implementation.

  3. Inheritance: Go supports inheritance-like behavior through the use of struct embedding, as described in my previous answer.

These are the main concepts of OOP that are supported in Go. By using these features, you can write clean, maintainable, and reusable code in Go, while taking advantage of the benefits of OOP.