loading...

How to let OOP world and dive in golang mindset?

gsvaldevieso profile image Guilherme Soares Valdevieso ・1 min read

Golang
Hello, Guys.

I've started to work with golang and I came from PHP(a good language to work with OO). But i've some questions about the world of golang. We've some good practices that are available for reading in some articles, blog posts and the official page. But, what is the guideline/good practices to build maintainable applications as we have in the OOP world like DDD, CQRS, Event-Sourcing, SOLID and other concepts. I know that we don't need to forget all these things, but how we can change it to be adapted to golang paradigm and structs.

I've found some repos, but i'm not fully satisfacted:

https://github.com/roblaszczak/go-cleanarch
https://github.com/CaptainCodeman/clean-go

Discussion

pic
Editor guide
Collapse
nektro profile image
Meghan (she/her)

Many OOP languages have a concept of classes. Go and other compiled-to-assembly languages have not done this to this day but Go is actually the closes OOP language that I've seen that is natively compiled.

As you've already seen, Go instead has structs. These are a special data type so that the compiler can right the assembly to be as memory efficient as possible since with structs, the compiler is going to be able to figure out exactly how many bytes an object will occupy.

On to functions. As you probably already know when you call a function the parameters to that function can either be passed-by-value or passed-by-reference. In native languages, pass-by-reference are called pointers. Pointers are a new data type type that most higher level languages are able to abstract away from but require a basic understanding to understand why Go, Rust, and C functions are defined the way they are. In a high level language, you can make a class with object methods that can use this. In Go, all functions are defined in the top level and "this" has to be defined as a pointer parameter. Kinda anyways. Let's see.

class Rectangle {
    function __construct(x, y, width, height) {
        this.x = x;
        this.y = y;
        this.w = width;
        this.h = height;
    }
    function area() {
        return this.w * this.h;
    }
}

$rect = new Rectangle(0, 0, 6, 4);
echo $rect.area() . "\n"; // 24

vs.

type Rectangle struct {
    width int
    height int
}

func (r *Rectangle) area() int {
    return r.width * r.height
}

func main() {
    rect := Rectangle{width: 6, height: 4}
    fmt.Println("area: ", rect.area()) // 24
}

See gobyexample.com/ for more!

Collapse
gsvaldevieso profile image
Guilherme Soares Valdevieso Author

Hi, Sean.

Thanks for your response.

You has tell me about the structure and how golang works when compared to OOP languages. But my main doubt is about the good practices when we are building applications, as the links that I put ahead.

Thank you again!

Collapse
nektro profile image
Meghan (she/her)

Hi, for SOLID, Go interfaces are actually really cool and I think this site would help a lot: gobyexample.com/interfaces

I also think this would be a good read, if you haven't read it already :D

Collapse
joncalhoun profile image
Jon Calhoun

I would definitely recommend checking out this article - medium.com/@benbjohnson/standard-p...

While it won't answer all of your questions, it should help you get an idea of where to start. Another thing you can do is just experiment; write code and see what does/doesn't work for you. Its okay to realize later that what you wrote originally wasn't the best way and to refactor your code. That's part of the learning process :)

Collapse
gsvaldevieso profile image
Guilherme Soares Valdevieso Author

Thank you, Jon. That's what i was talking about.