

Shifting from JavaScript to Go, Iβve encountered a few βwhaaat?!β moments that eventually became βhaaa, ok!β moments. To put it more eloquently, if youβre a JavaScript/TypeScript developer and thinking about trying Go, this article is for you.
Working at Orbs, a blockchain for consumer apps, Iβve taken part in contributing to the reference implementation in TypeScriptβββIβve implemented a consensus algorithm from scratch. Recently, weβve started working on the reference implementation in Go language. My task is to port the consensus algorithm to Goβββyou can see the converted code in progress here.
for
loops and if
statements. Iβd say that Go is something between C and JavaScriptThere are many other differences in addition to the following list, but I have found that these are the most interesting for developers that come from JavaScript background.
One key thing you should understand is that Go is very opinionated. For example regarding syntax, in Go you MUST put the {
in your if
statement at the same line. Not doing so will result in a compilation error!
Go is structured in a way that classes, inheritance, and polymorphism are not possible (or at least very difficult). What started as βI donβt like that!β became βHmmm, maybe thatβs even better!β when I understood that Go is pushing you to use composition instead of inheritance.
In Go, there are many shortcuts. It almost feels as though anything that can be written in Go with fewer characters.
// The long way
var age Int = 42
// The short way
age := 42
This makes your learning curve steeper, but if you stick with Go, youβll (probably) eventually like it.
Go is typed, but no generics. This means that you donβt have the lodash-like map/reduce functions. This βWhaaatβ did not end with βhaa okββ¦
It is planned for future versions of Go.
When I first installed Go and wanted to create a project, I thought that Iβd create a folder and put all my code there, build it, and runβ¦nope. This is not how Go usually operates. All your projects should be under the same global src
folder, under a folder unique to your project, usually your Github path (for example: ~/go/src/github.com/gilamran/PBFT-Typescript
)
There are many issues with this approach like the dependencies of your project being under the same src
folder and versions becoming a big issue.
Golang does support vendor
folder (Similar to node_modules
in JavaScript), but it will fetch the master
branchβ¦
There are many attempts to solve this, but know that currently dependencies in Go can be painful.
Golang loves tests, so much so that when the compiler detects test files (by file name _test.go), it will automatically exclude the test code when you build.
Before you dive in, let me give you a short preview of how we do things:
,
after the value.var arr = [3]int{
1,
2,
3,
}
{
of if
is at the same linevar ThisIsPublic = "You can access this variable from outside"
var privateValue = "Accessible only inside the current package"
// common types
var num int = 5
var pi float32 = 3.14
var name string = "Gil"
var isActive bool = true
// without initialisation
var num int // value is 0
var pi float32 // value is 0
var name string // value is ""
var isActive bool // value is false
var person Person // value is nil
// one liner
var one, two, three bool
var a, b, c = true, "Hi", 0.5 // you got the point
// short version with inference
num := 5 // `var` is omitted, and type is inffered
// you should be familiar with this
for i := 0; i < 100; i++ {
sum += i
}
// while
for sum < 1000 {
sum += sum
}
// infinite loop
for {
}
/// yep, no parenthesis
if age < 18 {
return true
} else {
return false
}
// switch no need to add `break`
switch state {
case "ACTIVE" :
deactivate()
case "INACTIVE":
activate()
default:
fmt.Printf("Unknown state")
}
// simple function
func add(a int, b int) int {
return a + b
}
// multiple return values
func swap(a, b int) (int, int) {
return b, a
}
// named return values
func namedResult(input int) (output int) {
output = 0
.
.
.
output = 42
.
.
.
return
}
Of course, this is the just tip of the iceberg. There are many other advanced things like channels, structs, and interfaces, but Iβll leave those for a future post.
Go is very interesting, very fast, and has an amazing GC. It really shines when talking about concurrency and it is very opinionated about how you write code. On the down side, the package management is not spectacular, and the lack of Generics is a bit annoying.
So, if you want to do something for the server, and you want it to work very fast, Go is a good choice.
Like what you read? Check out our GitHub projects and join the community:
Join the Orbs community:
Create your free account to unlock your custom reading experience.