• Classes

    A class in programming is like a group of people. For example, if you have a group of friends, each person in the group is called a "friend". In programming, we call each person in the group a "class". Just like your friends might all have different names and personalities, classes can also have different names and qualities. So when you write code, you can create different classes with different names and abilities to do things.

      • Object Classes

      • package main
        
        import "fmt"
        
        type Person struct {
        	name string
        	age  int
        }
        
        func (p Person) greet() {
        	fmt.Println("Hello my name is " + p.name)
        }
        
        func main() {
        	var human = Person{"John", 36}
        
        	fmt.Println(human.name) // John
        	fmt.Println(human.age)  // 36
        	human.greet()		   // Hello my name is John
        }
        
      • Trait Classes

      • package main
        
        import "fmt"
        
        type HasFlippers string
        
        type HasFeathers string
        
        func (h HasFlippers) swim() string {
        	return "can swim"
        }
        
        func (h HasFeathers) fly() string {
        	return "can fly"
        }
        
        type Duck struct {
        	HasFlippers
        	HasFeathers
        }
        
        type Seal struct {
        	HasFlippers
        }
        
        func main() {
        	var a Duck
        	fmt.Println(a.swim())
        	fmt.Println(a.fly())
        
        	var b Seal
        	fmt.Println(b.swim())
        }
        
      • Interface Classes

      • package main
        
        import (
        	"fmt"
        	"math"
        )
        
        type Shape interface {
        	Area() float64
        }
        
        type Rectangle struct {
        	width  float64
        	height float64
        }
        
        type Circle struct {
        	radius float64
        }
        
        func (r Rectangle) Area() float64 {
        	return r.width * r.height
        }
        
        func (c Circle) Area() float64 {
        	return math.Pi * c.radius * c.radius
        }
        
        func main() {
        	var a, b Shape
        	
        	a = Rectangle{width: 3, height: 5}
        	b = Circle{radius: 4}
        
        	fmt.Printf("area of a is %f", a.Area()) // area of a is 15.000000
        	fmt.Printf("area of b is %f", b.Area()) // area of b is 50.265482
        }
        
      • Struct Classes

      • package main
        
        import "fmt"
        
        type Person struct {
        	name string
        	age  int
        }
        
        func main() {
        	var human = Person{"John", 36}
        
        	fmt.Println(human.name) // John
        	fmt.Println(human.age)  // 36
        }
        
      • Enum Classes

      • package main
        
        import "fmt"
        
        const (
        	Sunday	int = 0
        	Monday	int = 1
        	Tuesday   int = 2
        	Wednesday int = 3
        	Thursday  int = 4
        	Friday	int = 5
        	Saturday  int = 6
        )
        
        func main() {
        	var day int = 4
        
        	if day == Saturday || day == Sunday {
        		fmt.Println("a weekend")
        	} else {
        		fmt.Println("a weekday") // a weekday
        	}
        }