Four Noteworthy Features of Golang In my previous post, I gave several reasons for why the Go programming language is popular. In this post, I’ll showcase specific features within Golang that are noteworthy. 1) Low-latency Garbage Collection Like other programming languages, Golang employs what are called Garbage Collectors (GC) to free up memory space. In particular, these GCs track “heap memory allocations, freeing up allocations that are no longer needed, and keeping allocations that are still in use.” The Golang GC is highly efficient, as it employs a tricolor mark-and-sweep algorithm that helps the GC run concurrently with the program. Go’s low-latency GC is engineered to help eliminate long program pauses common in other GCs. And as this post notes, with a concurrent GC “there is also the potential for running the GC in parallel on multiple processors.” 2) Memory Safety The term “memory safety” refers to being protected from memory access errors, whether caused by software bugs or security vulnerabilities. The Go programming language is especially safe in this respect. Critics note that while Go itself is safe, the standard compiler it uses may not be. They have also noted other notable situations in which memory access errors may occur. Nonetheless, Go employs a memory model that describes how goroutines are to use channels to safely share data. Rather than relying on a programmer to manage memory, Golang allows all memory allocation to be “managed by the language runtime, initialized before use, and bounds checked when necessary.” 3) Structural Typing In programming languages, structural typing allows class object names to be interchangeable. That’s because the names matter less than the code structure itself (unlike nominal typing that does not allow such changes). As Jared Carol notes, “Structural typing is like compile-time duck typing. It makes Go feel like a dynamic language, such as Ruby or Python.” He also notes that with Go, “you don’t declare that a type implements an interface. Instead, a type just implements the methods in the interface. This lack of ceremony makes interfaces feel very simple and informal.” 4) CSP-style concurrency Although somewhat fancy-sounding, Communicating Sequential Processes (CSP) simply refers to concurrent processes synchronizing with each other via their input and output. In Go, concurrency derives from the idea of using channels as first-class objects. While this may sound a bit heady, the key is understanding how Go concurrency works. For that, it’s necessary to describe how GoRoutines and Channels work within the code. And for good measure, descriptions involving Defer, Panic, and Recover are also given (as they affect concurrency as well). GoRoutines Golang conducts concurrent processing by using goroutines. A goroutine is simply a lightweight thread managed by the Go runtime. In general, one goroutine will evaluate f(x) and another goroutine will execute the f(x). For instance, when go f(0) is invoked, that function can be run asynchronously as another goroutine, Thus, Go allows any (independently managed) sequence of programmed instructions to be run separately. The alternative would be to create an entirely new coding sequence. As golang-book.com/ explains, The (program below) consists of two goroutines. The first goroutine is implicit and is the main function itself. The second goroutine is created when we call go f(0). Normally when we invoke a function our program will execute all the statements in a function and then return to the next line following the invocation. With a goroutine we return immediately to the next line and don’t wait for the function to complete. This is why the call to the Scanln function has been included; without it the program would exit before being given the opportunity to print all the numbers Channels Goroutines that are connected to each other can work to execute a program. First, a new channel is created that conveys the value type, i.e. make(chan string) or make(chan int). Next, the program sends a value into the channel ("ping" appears to be a favorite value in many channel examples). When the program runs, the explicit value will be sent from one goroutine to another. Likewise, this post conveys the value-type make(chan string)for a channel c. Here, the goroutine invokes a ‘greet’ function for the channel- go “greet” (c). Then the string value “John” is passed on to the c channel. As with above, however, these goroutines won’t execute until both sender and receiver are available to proceed. This limitation ensures that goroutines are executed synchronously. Defer The Go language also has what is called a defer statement. As the official Go website describes, defer invokes a function whose execution is deferred to the moment the surrounding function returns, either because the surrounding function executed a return statement, reached the end of its function body, or because the corresponding goroutine is panicking. Panic Panic is an exception in Go (an event that disrupts the normal flow of a program’s instructions during execution). Although Golang panics resemble C++ and Java exceptions, they’re reserved for run-time errors. Recover As one might suspect, the recover function enables a developer to regain control of a panic. For instance, say a panic occurs because no value exists for a name field. A recover function in the code would appear as func recoverName(). When invoked, this command could then be configured to return and print the value passed in a panic call. Developers should note that a call to recover must be executed within a deferred function if a goroutine panic sequence is to be stopped. Once these panic sequences have been successfully ended, the program will resume its normal execution Conclusion I’ve hopefully provided some insight to Go’s appeal (beyond the more general explanations given in yesterday’s post). Understanding how Go works is rather fascinating, as it reflects amazing engineering (simply consider an animated version of how Go’s Garbage Collection operates). Since writing about these concepts is far removed from what a developer contends with, suggestions and ideas for revision are welcome.