You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Go is not an object-oriented language in the traditional sense — it has no classes, no inheritance, and no constructors. Instead, Go uses structs to group data and methods to add behaviour to types. This approach is simpler than class-based OOP while remaining powerful and flexible.
A struct is a composite type that groups named fields:
type User struct {
ID int
Name string
Email string
Active bool
CreatedAt time.Time
}
// Named fields (recommended)
u := User{
ID: 1,
Name: "Alice",
Email: "alice@example.com",
Active: true,
}
// Positional (fragile — not recommended)
u := User{1, "Alice", "alice@example.com", true, time.Now()}
// Zero-value struct
var u User // all fields set to zero values
fmt.Println(u.Name) // Alice
u.Active = false // modify a field
Struct tags are metadata attached to fields, commonly used for JSON encoding, database mapping, and validation:
type User struct {
ID int `json:"id" db:"user_id"`
Name string `json:"name" db:"user_name"`
Email string `json:"email,omitempty"`
}
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.