A few weeks ago I started reading abut this new trending programming language named Go, I created a few pet projects with this shiny new programming language and here is how I feel about it, I’m not a go expert and this post doesn’t try to be technical

+ Goroutines

Think of Goroutines as something like threading in python (they are not the same but let’s say they are)

As a python developer I always tried to avoid threading in my code. I think threaing in python is complicated and error prune. Also working with global variables and locking can become confusing. So personally I preferred using celery instead of starting new threads.

Now here is a sample go code using Goroutines

package main

import (
    "fmt"
    "time"
)

var done = make(chan bool)

func worker(wid int) {
    fmt.Println(wid, "started")
    time.Sleep(time.Second)
    fmt.Println(wid, "ended")
    done <- true
}

func main() {
    // run workers
    for i := 0; i < 5; i++ {
        go worker(i)
    }

    // wait for workers to end
    for i := 0; i < 5; i++ {
        <-done
    }
}
  • Calling a Goroutine is very simple, just prefix your function call with go: go worker(i), It’s easy and understanble
  • With channels you can simply transfer data between goroutines
  • there is a mutex module, which helps you with locking variables (to avoid conflicts)

And did I mention that python threading doesn’t support multi-core execution? but goroutine automatically does that, so no extra code needed :-)

There is more about goroutines but I’m not going to get deeper.

? There are no exceptions!

You read a file in python like this:

try:
    content = open("file.txt", "r").read()
    print(content)
except Exception:
    print("some error")

Here is the go code for that

package main

import (
    "fmt"
    "io/ioutil"
)

func main() {
    dat, err := ioutil.ReadFile("file.txt")

    if err != nil {
        fmt.Println(err)
    } else {
        fmt.Print(string(dat))
    }
}

In go, errors are returned values, so if you want to check if some error happened you have to check the returned value (instead of catching exceptions)

+ gofmt

There is a tool named gofmt which formats your go codes. and most editors enforce that to your code. so if you check a few repos on github you will see how similarly all go codes are formatted.

There are similar tools for python too, pep8, autopep8, flake8 and many more, but these tools are always optional and many people do not care about formatting their code.

+ Autocompletion

As a person mostly coded in scripting languages like python, php, node, shell, … I was amazed by the autocompletion support in vscode for go. (I know it’s a benefit of any static typed language, not just go)

- Maturity

I’m not saying go is not a mature language or it’s buggy, there are many in-production tools written in go.

But python has a bigger amount of 3rd party modules, the syntax is more stable (the irony is python2 vs python3) and there is a higher chance to see a massive change in go language itself compared to python language

- Modules, (packages? I don’t know)

You want to use third party packages? or maybe put some code in a subdirectory? I found these simple actions very confusing. what is GOROOT? what about GOPATH? these things don’t make sense to me. why there are so many package manager tools? and the most confusing part for me was how people import a sub-directoy in their own package. the import is something like this

import "github.com/user/repo/subdir"

I’m not saying it’s bad, just saying for someone with with pip and npm background, it felt really confusing