No al cierre de webs
TreeWeb::Artículos::Golang::Breaking golang: static methods
Permalink: 19/02/2016

Breaking golang: static methods

Golang is a nice language, with a new concept of `method`. When your brain is working with typical object oriented designs, you could end up viewing the world with object oriented eyes, inheritance, polymorphism, object hierarchy, abstract methods... My brain is partially broken in that way so, sometimes, I get stucked forcing the language to a typical object oriented design.

In my opinion, Golang is not object oriented but data oriented. If we think carefully, all we do turn around data. The thing called `method` is a simply data resolution scope.
    1 package main
    3 import "fmt"
    5 type Say func() string
    7 type Human struct {
    8     Name     string
    9     SayHello Say
   10 }
   12 func main() {
   14     h := &Human{
   15         Name: "John",
   16         SayHello: func() string {
   17             return "Hello, I am a human"
   18         },
   19     }
   20     fmt.Println("Human says:", h.SayHello())
   22     // Allow access to h
   23     h.SayHello = func() string {
   24         return "Hello, I am a human, My name is " + h.Name
   25     }
   27     fmt.Println("Human says:", h.SayHello())
   28 }

We can also find an `interface` construction. Why? It is an interesting crazy merge between OO, duck typing and evilness. Why attributes cannot be defined inside an interface!

Warning: At this moment, I am being treated for objetorientedgitis and govilness.
Powered by TreeWeb
© TreeWeb 2010. Todos los derechos reservados