---
title: "Discussion on \"Top 5 Lessons I learned while working with Go for two years\" | Hashnode"
description: "Discussion on \"Top 5 Lessons I learned while working with Go for two years\". So I have been writing Go services for like two years now, both professionally and as personal projects. Using a certain language in numerous projects over an extended period of time allows you to make mistakes, fix them, realize it's still not the b..."
url: "https://hashnode.com/posts/top-5-lessons-i-learned-while-working-with-go-for-two-years/60a8fb2e5379f65d10b5f642"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Top 5 Lessons I learned while working with Go for two years](https://sayedalesawy.hashnode.dev/top-5-lessons-i-learned-while-working-with-go-for-two-years?utm_source=hashnode&utm_medium=feed)

So I have been writing Go services for like two years now, both professionally and as personal projects. Using a certain language in numerous projects over an extended period of time allows you to make mistakes, fix them, realize it's still not the b...

May 22, 2021 · 12 min read · sayedalesawy.hashnode.dev  
Updated Jun 2, 2021  
Read the article

-   [#go](https://hashnode.com/tag/go)
-   [#programming-tips](https://hashnode.com/tag/programming-tips)
-   [#concurrency](https://hashnode.com/tag/concurrency)
-   [#learning](https://hashnode.com/tag/learning)

## Written by

[Sayed Alesawy](https://hashnode.com/@sayedalesawy)

@sayedalesawy · Cairo, Egypt

  
[View profile](https://hashnode.com/@sayedalesawy)

Lead Software Engineer @ Instabug

Lead Software Engineer at Instabug | Backend team. Passionate about large scale server side applications and distributed systems. Cairo University - Faculty of Engineering grad.

## Responses 12

Markdown supported · ⌘↵ to post

[مصطفى الصعيدي](https://hashnode.com/@Elsaidi)

[Jun 16, 2024](https://hashnode.com/posts/top-5-lessons-i-learned-while-working-with-go-for-two-years/60a8fb2e5379f65d10b5f642/comment/666f50d89647eb5b9ff222b1)

جميلة

[Yosry Ahmed](https://hashnode.com/@yosrym93)

[Jul 10, 2022](https://hashnode.com/posts/top-5-lessons-i-learned-while-working-with-go-for-two-years/60a8fb2e5379f65d10b5f642/comment/62ca408fe62bf98e0fb0967d)

Great Article! Very informative.

Just a quick note, you can’t actually intercept SIGKILL (or SIGSTOP for that matter), I believe you meant SIGTERM or SIGINT in your example in (4).

[Mike Graf](https://hashnode.com/@maerf0x0)

[Sep 28, 2021](https://hashnode.com/posts/top-5-lessons-i-learned-while-working-with-go-for-two-years/60a8fb2e5379f65d10b5f642/comment/6153591f414f78089df3b4c7)

Thanks for sharing, we'll especially want people to ensure they understand CSP (tony Hoare) , and know about the go proverbs ( a source of the sharing memory quote: [go-proverbs.github.io](https://go-proverbs.github.io/) )

Good video on the topic: [youtube.com/watch](https://www.youtube.com/watch?v=oV9rvDllKEg)

A lot of go problems seem to stem from not understanding how to model their program concurrently

[Oleksiy](https://hashnode.com/@night-codes)

[Jun 13, 2021](https://hashnode.com/posts/top-5-lessons-i-learned-while-working-with-go-for-two-years/60a8fb2e5379f65d10b5f642/comment/60c653704e04b16f767310ca)

Is it just me or does **sync.Once** compound the problem? While **&sql.Open(...)** is running, all other DBConnection will return nil?

[Oleksiy](https://hashnode.com/@night-codes)

[Jun 13, 2021](https://hashnode.com/posts/top-5-lessons-i-learned-while-working-with-go-for-two-years/60a8fb2e5379f65d10b5f642/comment/60c653704e04b16f767310ca#reply-60c659df1679ed6f51a28571)

[Sayed Alesawy](https://hashnode.com/@sayedalesawy) As far as I understand, in this case sql.Open is just for example, which means there could be any other function with indefinite execution time.

I would like to return to the problem stated in paragraph 2 (without init function): ...Consider the case where 2 goroutines are calling the function DBConnection() at the same time. It's possible that the first goroutine reads the value of dbInstance and finds it nil and then proceeds to create a new connection...

Does dbOnce.Do block the rest of the function's calls for the execution time, or just let them pass by and return nil?

[Sayed Alesawy](https://hashnode.com/@sayedalesawy)

[Jun 13, 2021](https://hashnode.com/posts/top-5-lessons-i-learned-while-working-with-go-for-two-years/60a8fb2e5379f65d10b5f642/comment/60c653704e04b16f767310ca#reply-60c66d471679ed6f51a28631)

[Oleksiy](https://hashnode.com/@night-codes) Oh, I understand what you mean now. That's a really nice case to think about.

As far as I know, calls to sync.Once won't return until the function f() inside the Do function returns. So if multiple goroutines call it concurrently, they will block waiting until the first call returns, then they will be executed, but of course they won't re-execute Once again.

I wrote this code: [play.golang.org/p/ftvpEyhHDio](https://play.golang.org/p/ftvpEyhHDio) to make sure and try it out. You can try changing the value of Sleep at line 22 to something bigger and you will see that the routines are blocked.

Considering this behavior, I still think it's good for initializations.

[Oleksiy](https://hashnode.com/@night-codes)

[Jun 13, 2021](https://hashnode.com/posts/top-5-lessons-i-learned-while-working-with-go-for-two-years/60a8fb2e5379f65d10b5f642/comment/60c653704e04b16f767310ca#reply-60c68e851679ed6f51a286e4)

[Sayed Alesawy](https://hashnode.com/@sayedalesawy) Ah, great, then the question is off the table. Yes, I've now studied the Once.Do implementation, it's fun and seems to be specifically designed to lock other calls until the return from the function happens.

[Andrey Panin](https://hashnode.com/@fenhoya)

[Jun 2, 2021](https://hashnode.com/posts/top-5-lessons-i-learned-while-working-with-go-for-two-years/60a8fb2e5379f65d10b5f642/comment/60b75d5f25dc276ffb93fcf9)

The approach with channels from (1) looks dubious to me. You have omitted the important part explaining it by clarity, but what is missing is important for understanding of the concept. Maybe it's just me.

You have to call `server.Run()` in a goroutine in (4), otherwise your code will block at this method.

[Sayed Alesawy](https://hashnode.com/@sayedalesawy)

[Jun 2, 2021](https://hashnode.com/posts/top-5-lessons-i-learned-while-working-with-go-for-two-years/60a8fb2e5379f65d10b5f642/comment/60b75d5f25dc276ffb93fcf9#reply-60b7bd2b2102c86fd6d18dff)

Yes, you are correct about 4, it was a typo, I corrected it. Thank you!

About 1, I dunno, might be up to taste but I have seen it in a lot of places and written by people who are go contributors actually. It's definitely less verbose but if you are very familiar with channels, it's as understandable in my opinion.

[George](https://hashnode.com/@george3000)

[Jun 1, 2021](https://hashnode.com/posts/top-5-lessons-i-learned-while-working-with-go-for-two-years/60a8fb2e5379f65d10b5f642/comment/60b6406cf4a8435c0ecc4629)

Singletons are horrible advice, don't follow that pattern. Anything that creates a static, package level variable is bad.

[George](https://hashnode.com/@george3000)

[Jun 2, 2021](https://hashnode.com/posts/top-5-lessons-i-learned-while-working-with-go-for-two-years/60a8fb2e5379f65d10b5f642/comment/60b6406cf4a8435c0ecc4629#reply-60b783862102c86fd6d18748)

[Andrey Panin](https://hashnode.com/@fenhoya) [misko.hevery.com/code-reviewers-guide/flaw-brittl…](http://misko.hevery.com/code-reviewers-guide/flaw-brittle-global-state-singletons/)

[Sayed Alesawy](https://hashnode.com/@sayedalesawy)

[Jun 2, 2021](https://hashnode.com/posts/top-5-lessons-i-learned-while-working-with-go-for-two-years/60a8fb2e5379f65d10b5f642/comment/60b6406cf4a8435c0ecc4629#reply-60b7bd872102c86fd6d18e07)

I know there is a somewhat compelling argument against singleton in general. I am not sure how could I replace it tho and get the same effect? I will check the article you mentioned. Thanks for the comment!

[George](https://hashnode.com/@george3000)

[Jun 2, 2021](https://hashnode.com/posts/top-5-lessons-i-learned-while-working-with-go-for-two-years/60a8fb2e5379f65d10b5f642/comment/60b6406cf4a8435c0ecc4629#reply-60b7cd5125dc276ffb940d3a)

[Sayed Alesawy](https://hashnode.com/@sayedalesawy) Replace it with a regular struct which accepts the dependency on it.

If it gets complex you can write a factory method on the struct.

[Maxim Pak](https://hashnode.com/@xFox)

[May 28, 2021](https://hashnode.com/posts/top-5-lessons-i-learned-while-working-with-go-for-two-years/60a8fb2e5379f65d10b5f642/comment/60b1587dfb51fd32a4dbf700)

As far as I know, in most cases singletons are considered an anti-pattern. It's probably better to explicitly pass \*sql.DB object around

[Sayed Alesawy](https://hashnode.com/@sayedalesawy)

[May 29, 2021](https://hashnode.com/posts/top-5-lessons-i-learned-while-working-with-go-for-two-years/60a8fb2e5379f65d10b5f642/comment/60b1587dfb51fd32a4dbf700#reply-60b1ef809b1e2e1dccbce010)

I am not really sure why do you think so. I mean my rationale is that you will probably need to do some initialization of some sort, like maybe pass connection configurations or something like that. You will probably do it in an init method or something, then after that you can use the connection variable. Looks like if somebody forgot to init, they will run into problems and here comes the usefulness of having a wrapper method. And if you have a wrapper method and it's not singleton, that will be problematic as well.

I am interested to know why do you think it's an anti pattern or if you have a resource that supports that. Thanks for the thought tho!

[Kevin Wan](https://hashnode.com/@kevwan)

[May 28, 2021](https://hashnode.com/posts/top-5-lessons-i-learned-while-working-with-go-for-two-years/60a8fb2e5379f65d10b5f642/comment/60b11308fb51fd32a4dbef1b)

Great article! I've been used c/c++/c#/java/python for many years, and using go since 2013. All the mentioned lessons I have met before. Thanks for sharing!

[Phillip Ninan](https://hashnode.com/@phillip_ninan)

[May 24, 2021](https://hashnode.com/posts/top-5-lessons-i-learned-while-working-with-go-for-two-years/60a8fb2e5379f65d10b5f642/comment/60abbdc361f5f47b572b5e81)

Very cool read! I have heard great things about Go. This is a cool overview of some of its functionality. It seems to handle concurrency very well!

Thank you!

[Sayed Alesawy](https://hashnode.com/@sayedalesawy)

[May 24, 2021](https://hashnode.com/posts/top-5-lessons-i-learned-while-working-with-go-for-two-years/60a8fb2e5379f65d10b5f642/comment/60abbdc361f5f47b572b5e81#reply-60abe97a61f5f47b572b62d1)

Thank you! Yes, Go is really great for writing concurrent applications and it's also blazing fast and doesn't really take that much development time to build, so all in all, quite an awesome language.

[Wei Lun](https://hashnode.com/@weilun)

[May 24, 2021](https://hashnode.com/posts/top-5-lessons-i-learned-while-working-with-go-for-two-years/60a8fb2e5379f65d10b5f642/comment/60ab04c620d1f45ceb7f07d5)

Great! Can't wait for part two

[Sayed Alesawy](https://hashnode.com/@sayedalesawy)

[May 24, 2021](https://hashnode.com/posts/top-5-lessons-i-learned-while-working-with-go-for-two-years/60a8fb2e5379f65d10b5f642/comment/60ab04c620d1f45ceb7f07d5#reply-60ab637620d1f45ceb7f135d)

Thank you! Hopefully, I will get to it soon.

[Shaswat Singh](https://hashnode.com/@ShaswatSingh19)

[May 23, 2021](https://hashnode.com/posts/top-5-lessons-i-learned-while-working-with-go-for-two-years/60a8fb2e5379f65d10b5f642/comment/60aa0db720d1f45ceb7ef3c2)

nice article by the way...

[Sayed Alesawy](https://hashnode.com/@sayedalesawy)

[May 23, 2021](https://hashnode.com/posts/top-5-lessons-i-learned-while-working-with-go-for-two-years/60a8fb2e5379f65d10b5f642/comment/60aa0db720d1f45ceb7ef3c2#reply-60aa0f7a5379f65d10b60bd0)

Thank you! Glad you liked it.

[Pavan Chindukuri](https://hashnode.com/@ghana7989)

[May 23, 2021](https://hashnode.com/posts/top-5-lessons-i-learned-while-working-with-go-for-two-years/60a8fb2e5379f65d10b5f642/comment/60a9d69e20d1f45ceb7eec2e)

Now, Your article made me start learning GO. But I have no experience with strictly typed languages.

[Sayed Alesawy](https://hashnode.com/@sayedalesawy)

[May 23, 2021](https://hashnode.com/posts/top-5-lessons-i-learned-while-working-with-go-for-two-years/60a8fb2e5379f65d10b5f642/comment/60a9d69e20d1f45ceb7eec2e#reply-60aa0f6320d1f45ceb7ef411)

That's great! I recommend checking this tutorial: [golangbot.com/learn-golang-series](https://golangbot.com/learn-golang-series/) along with the go tour here: [tour.golang.org/welcome/1](https://tour.golang.org/welcome/1) and anything else you need later, there is probably a godoc for it.

[Kieran Roberts](https://hashnode.com/@kieran6roberts)

[May 22, 2021](https://hashnode.com/posts/top-5-lessons-i-learned-while-working-with-go-for-two-years/60a8fb2e5379f65d10b5f642/comment/60a937aa20d1f45ceb7ee418)

I really enjoyed reading your article [Sayed Alesawy](https://hashnode.com/@sayedalesawy). Go is not a language I am familiar with but it seems really interesting. Thanks for sharing!

[Sayed Alesawy](https://hashnode.com/@sayedalesawy)

[May 22, 2021](https://hashnode.com/posts/top-5-lessons-i-learned-while-working-with-go-for-two-years/60a8fb2e5379f65d10b5f642/comment/60a937aa20d1f45ceb7ee418#reply-60a938aa5379f65d10b5fbca)

Thank you! Glad you liked it.
