EZ

Eduzan

Learning Hub

Eduzan
Eduzan / Go Programming Language

Object Oriented Programming

Is Go Object Oriented?

Go is not considered a pure object-oriented programming language. However, as outlined in Go’s FAQs, it incorporates features that allow object-oriented programming to some extent.

Does Go support object-oriented programming?

Yes and no. While Go includes types, methods, and enables an object-oriented style of programming, it does not feature a traditional type hierarchy. Instead, Go utilizes the concept of “interfaces,” offering a more flexible and generalized approach. Additionally, Go supports embedding types into other types, achieving functionality similar—but not identical—to subclassing. Methods in Go are versatile and can be defined for any kind of data, including basic types like integers, not just structs (classes).

Upcoming sections will explore how Go implements object-oriented concepts, which differ in approach compared to languages like Java or C++.

Structs Instead of Classes

In Go, structs act as a substitute for classes. Structs can have associated methods, allowing data and behaviors to be bundled together in a manner similar to a class.

Let’s dive into an example to understand this concept better.

We’ll create a custom package to demonstrate how structs can effectively replace classes.

Steps to Create the Example

  1. Set Up the Directory
    • Create a subfolder in ~/Projects/ named oop.
    • Inside the oop directory, initialize a Go module by running:
go mod init oop

2. Create a Subfolder and Add Files

  • Add a subfolder named product inside the oop folder.
  • Inside product, create a file named product.go.

Folder Structure:

├── Projects
│   └── oop
│       ├── product
│       │   └── product.go
│       └── go.mod

3. Code for product.go
Replace the contents of product.go with:

package product

import "fmt"

type Product struct {
    Name      string
    Price     float64
    Stock     int
}

func (p Product) StockValue() {
    fmt.Printf("The total value of %s stock is %.2f\n", p.Name, p.Price*float64(p.Stock))
}
  • The Product struct bundles product data: NamePrice, and Stock.
  • The StockValue method calculates the total value of the stock for the product.

4. Create main.go
Add a new file, main.go, in the oop folder.

Folder Structure:

├── Projects
│   └── oop
│       ├── product
│       │   └── product.go
│       ├── go.mod
│       └── main.go

5. Code for main.go
Replace the contents of main.go with:

package main

import "oop/product"

func main() {
    p := product.Product{
        Name:  "Laptop",
        Price: 50000.0,
        Stock: 10,
    }
    p.StockValue()
}

6. Run the Program
Use the following commands:

go install
oop

Output:

The total value of Laptop stock is 500000.00

The New() Function as a Constructor

The program above works fine, but what happens when a Product struct is initialized with zero values? Modify main.go as follows:

package main

import "oop/product"

func main() {
    var p product.Product
    p.StockValue()
}

Output:

The total value of  stock is 0.00
End of lesson.