DEV Community

Harry Douglas
Harry Douglas

Posted on

How OOP works in Golang

I'm quite beginner here in GO but I wanna share what I've learnt about it today! Lets start without losing time. If you are coming from another OOP friendly language, you already know what is a class.
In Go we have the same thing but a bit different:

type Animal struct {
    Name string
}
Enter fullscreen mode Exit fullscreen mode

This is the most basic class we have. Then we can add methods to it:

func (Animal) fly(){
    fmt.Println("Flying...")
}
Enter fullscreen mode Exit fullscreen mode

The syntax seems a bit complicate to me as someone from PHP world but not too bad!

Share your comments with me to feel that I am not alone here with this new stuff :)

Top comments (0)