Classes and objects form the foundation of Object-Oriented Programming (OOP).
A class is a blueprint or template. It defines the attributes (fields) and behaviors (methods) of an object.
Go does not have traditional classes, but you can achieve similar behavior using struct
and methods.
Here's a simple example:
type Car struct {
// Attributes
Color string
Make string
Model string
Year int
// Method to display car details
func () DisplayInfo() {
fmt.Println("Car Make: " + Make)
fmt.Println("Car Model: " + Model)
fmt.Println("Car Year: " + Year)
fmt.Println("Car Color: " + Color)
}
}
- Attributes: The struct
Car
has four attributes that describe its state:Color
,Make
,Model
, andYear
. - Methods: The
DisplayInfo
function is responsible for showcasing the car details.
An object is an instance of a struct. When you create an object, you are bringing the blueprint of the struct into reality. It consists of state and behavior defined by the struct, with each object holding its own copy of the data.
To create an object, you instantiate the struct Car
.
Here's how you can instantiate objects from the Car
struct:
func main() {
// Creating an object of the Car class
car1 := Car{"Red", "Toyota", "Corolla", 2020}
car2 := Car{"Blue", "Ford", "Mustang", 2021}
// Displaying information of each car
car1.DisplayInfo()
fmt.Println("-----------------")
car2.DisplayInfo()
}
- Instantiation: The struct
Car
is used to create an object, which allocates memory for it. - Initialization: The struct instance holds its own set of values.
- Reference: The object is referenced using a variable (
car1
,car2
) that holds its memory location.