Object Oriented Go

Composition over Inheritance

class Vehicle
{
public:
void drive() {}
int getWheels() { return wheels }
private:
int wheels;
};

class Car: public Vehicle
{
public:
void drive() { /*Override with some behvaior */}
void honk() { /* Some honking Logic */ }
};
type Person struct {
Name string
JobTitle string
ShoeSize float32
}
func (p Person) SayHello() {
fmt.Println(“Hello, I’m “, p.Name)
}
func (p Person) GetJobTitle() {
fmt.Println(“I’m a “, p.JobTitle)
}
type Doctor struct {
Person
Degree string
}
func (d Doctor) SayHello() {
fmt.Println(“Hello, I’m Dr.”, d.Name, “, “, d.Degree)
}
func (d Doctor) Diagnose() {
fmt.Println(“It’s Lupus.”)
}

What are interfaces?

Interfaces for Polymorphism

package mainimport “fmt”type Animal interface {
Type() string
Swim() string
}
type Dog struct {
Name string
Breed string
}
type Frog struct {
Name string
Color string
}
func main() {
f := new(Frog)
d := new(Dog)
zoo := […]Animal{f, d}
for _, a := range zoo {
fmt.Println(a.Type(), “ can “, a.Swim())
}
}
func (f *Frog) Type() string {
return “Frog”
}
func (f *Frog) Swim() string {
return “Kick”
}
func (d *Dog) Swim() string {
return “Paddle”
}
func (d *Dog) Type() string {
return “Doggie”
}
type Person struct {
Name string
Age int
ShoeSize float32
}
type PeopleByShoeSize []Personfunc (p PeopleByShoeSize) Len() int {
return len(p)
}
func (p PeopleByShoeSize) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func (p PeopleByShoeSize) Less(i, j int) bool {
return (p[i].ShoeSize < p[j].ShoeSize)
}
func main() {
people := []Person{
{
Name: “Person1”,
Age: 25,
ShoeSize: 8,
},
{
Name: “Person2”,
Age: 21,
ShoeSize: 4,
},
{
Name: “Person3”,
Age: 15,
ShoeSize: 9,
},
{
Name: “Person4”,
Age: 45,
ShoeSize: 15,
},
{
Name: “Person5”,
Age: 25,
ShoeSize: 8.5,
},
}
fmt.Println(people)
sort.Sort(PeopleByShoeSize(people))
fmt.Println(people)
}
/* OUTPUT
[{Person1 25 8} {Person2 21 4} {Person3 45 15} {Person4 25 8.5}]
[{Person2 21 4} {Person1 25 8} {Person4 25 8.5} {Person3 45 15}]
*/

Database Drivers

Engineer @ DoubleVerify

Engineer @ DoubleVerify