Shifting from to Go, I’ve encountered a few “ ” moments that eventually became “ ” moments. To put it more eloquently, if you’re a JavaScript/TypeScript developer and thinking about trying Go, this article is for you. JavaScript whaaat?! haaa, ok! Working at , a for consumer apps, I’ve taken part in contributing to the reference implementation in TypeScript — I’ve implemented a 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 . Orbs blockchain consensus algorithm here Similarities Like JavaScript, Go uses Garbage Collector. You don’t have to do any memory allocation/disposal. Functions play a big part of your day to day development. Variables and functions have scope. You can use closure in functions. After all, it’s a very simple language, and like JavaScript you define variables, structures, functions and do your loops and statements. I’d say that Go is something between C and JavaScript for if (Big) Differences There 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. Go is statically typed, and will detect type errors on compilation I know that Garbage Collector was in the “Similarities” above, but in Go there’s a big difference: GC is No lags! amazingly FAST. JavaScript is based on one main thread that runs your code (The event-loop) and several other threads that do external IO. Get ready. to grasp at first, but once you get it, you’ll have great power in your hands. (The world is progressing to more and more cores) While in Go, concurrency is king! It is not easy It is so fun to write JavaScript and just run it. JavaScript is interpreted, and code is compiled just before it runs, but who really does that? Today, the JavaScript tool-chain involves many code analysis tools and even compilation like Babel or TypeScript. As mentioned above, Go is compiled. Go is using pointers. Yes! Pointers. BUT WAIT! Don’t stop reading.In order to be able to do GC properly and avoid reading/writing out of memory, Go pointers do not allow any arithmetics on pointers. You can’t do p++ on a pointer to move it to the next item in an array, like you would do in C. But you do have all the other benefits of pointers (References) Go is than JavaScript! Depending on what you measure, it can even be x10 times faster faster Go is better suited for the server, while JavaScript is, well, everywhere “Whaaat!? Haaa, ok” One key thing you should understand is that Go is For example regarding syntax, in Go you put the in your statement at the same line. Not doing so will result in a compilation error! very opinionated. MUST { if No OOP Go is structured in a way that classes, inheritance, and polymorphism are not possible (or at least very difficult). What started as “ ” became “ ” when I understood that Go is pushing you to use composition instead of inheritance. I don’t like that! Hmmm, maybe that’s even better! Shortcuts In Go, there are many shortcuts. It almost feels as though anything that can be written in Go with fewer characters. // The long wayvar age Int = 42 // The short wayage := 42 This makes your learning curve steeper, but if you stick with Go, you’ll (probably) eventually like it. Map/Reduce Go is typed, but . This means that you don’t have the lodash-like map/reduce functions. This “ ” did not end with “ ”…It is for future versions of Go. no generics Whaaat haa ok planned Packages 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 folder, under a folder unique to your project, usually your Github path (for example: ) src ~/go/src/github.com/gilamran/PBFT-Typescript There are many issues with this approach like the dependencies of your project being under the same folder and versions becoming a big issue. src Golang does support folder (Similar to in JavaScript), but it will fetch the branch… vendor node_modules master There are many attempts to solve this, but know that currently dependencies in Go can be painful. Tests Golang loves tests, so much so that when the compiler detects test files (by file name ), it will automatically exclude the test code when you build. _test.go Syntax Before you dive in, let me give you a short preview of how we do things: Basic rules Lines don’t end with a semicolon. Last item in an array must have the after the value. , var arr = [3]int{1,2,3,} of is at the same line { if public/private is defined by whether the first letter of the name is capitalized or not, respectively. var ThisIsPublic = "You can access this variable from outside"var privateValue = "Accessible only inside the current package" Basic types // common typesvar num int = 5var pi float32 = 3.14var name string = "Gil"var isActive bool = true // without initialisationvar num int // value is 0var pi float32 // value is 0var name string // value is ""var isActive bool // value is falsevar person Person // value is nil // one linervar one, two, three boolvar a, b, c = true, "Hi", 0.5 // you got the point // short version with inferencenum := 5 // `var` is omitted, and type is inffered Loops // you should be familiar with thisfor i := 0; i < 100; i++ {sum += i} // whilefor sum < 1000 {sum += sum} // infinite loopfor {} Flow control /// yep, no parenthesisif 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")} Functions // simple functionfunc add(a int, b int) int {return a + b} // multiple return valuesfunc swap(a, b int) (int, int) {return b, a} // named return valuesfunc 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. Conclusion 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: _Orbs platform reference implementation in Go. Contribute to orbs-network/orbs-network-go development by creating an…_github.com orbs-network/orbs-network-go _Orbs platform protocol and service specifications. Contribute to orbs-network/orbs-spec development by creating an…_github.com orbs-network/orbs-spec _Monorepo for Orbs platform reference implementation — orbs-network/orbs-network-typescript_github.com orbs-network/orbs-network-typescript _Go implementation of Lean Helix consensus algorithm - orbs-network/lean-helix-go_github.com orbs-network/lean-helix-go Join the Orbs community: GitHub: https://github.com/orbs-network : Telegram https://t.me/orbs_network : Twitter https://twitter.com/orbs_network : Reddit https://www.reddit.com/r/ORBS_Network/ : Read the Orbs white papers https://www.orbs.com/white-papers
Share Your Thoughts