Structs and methods
โ Report an issue with this lessontype Course struct {
Title string
Price float64
}
func (c Course) Describe() string {
return fmt.Sprintf("%s -- $%.2f", c.Title, c.Price)
}
course := Course{Title: "Go Mastery", Price: 149.99}
fmt.Println(course.Describe())
Go has no classes -- a method is just a function with a "receiver"
(c Course) attached. There's no inheritance either; Go favors
composition, embedding one struct inside another to share behavior.
Try it yourself
Exercise: Add a Describe() method to Course with a receiver, then call it.
Expected output: open-ended โ there's no single correct output here, just get your code running without errors.
Run your code and get it working before marking this lesson complete.
// that was the last free lesson
8 more lessons โ including Capstone project: a course catalog CLI โ plus a certificate are waiting.
Unlock the full course โ $149.99